Learning Swift

When Swift was announced in 2014, I was as surprised as anyone. There were only a handful of folks working at Apple who had advanced knowledge of it, but like the rest of the Apple development community we were intensely curious about the new language. After a quick investigation, I decided my work on iTunes Connect for iOS wouldn’t benefit and I contented myself to allow it to mature.

When I decided to leave Apple this summer, I realised it was genuinely time to learn Swift. From all accounts Swift was finally ready for Prime Time with ABI Stability and no more source-breaking changes. I’ve always picked up new languages in the past by building something with them. Caitlin and I had been brainstorming an idea for a new App for quite a while; so this would be an ideal opportunity. I dove right in.

First let me say I am extremely grateful for the patience and tolerance of the Seattle Xcoders. I’m actually a bit surprised they haven’t banned me after all my complaining. As a software engineer with more than 30 years of experience, learning Swift is not like learning other languages. In Swift there is often One True Way™ and to deviate from The Way means frustration at best if not outright failure. This often shows in small things like familiar patterns being missing, because as Daniel Steinberg jokingly puts it, “You can’t be trusted with it.”

This weekend I tried converting a tool from Ruby to Swift — largely to be able to take advantage of CoreGraphics without jumping through a lot of extra hoops. (Plus it would help expand my understanding of Swift.) The first feature I started working on was the DependencyList which tracks what files have changed from one execution to another. It’s nothing complex, but essential to efficient operation.

As you’d expect the DependencyList keeps track of whether there are changes. As new files are scanned, the @changes property is updated as follows:

if old_dependency
  @changed |= (old_dependency.digest != new_dependency.digest) || (old_dependency.mod_time < new_dependency.mod_time)
else
  @changed |= true

When I tried to replicate this same logic in Swift, I ran up against an interesting road block. The |= operator isn’t valid for boolean values. For example:

if let oldDependency = oldDependency {
    self.changed |= newDependency.hasChanged(from: oldDependency)
}
else {
    self.changed |= true
}

The compiler tells me the following: Binary operator '|=' cannot be applied to two 'Bool' operands. I’m surprised this isn’t permitted in Swift, which to my eye seems to value brevity over clarity in many cases.4

It’s not a big thing to use foo = foo || boolean-expression instead of foo |= boolean-expression, but one of the things I’ve always taken for granted about learning a new language5 is using patterns I’m already familiar with even if they aren’t the native idioms. This was very much the case when I started learning Java, Python, and Ruby. My code didn’t become idiomatic for a long time, but it worked nearly right away.

The second stumbling block I encountered was with Encodable.6 The new Swift Encodable/Decodable protocols are pretty amazing. They can’t do everything, but they’re still a great step forward. I figured JSONEncoder would make saving my DependencyList a super simple matter. However, I ran into something… odd.

Consider the following code:

struct Dependency: Encodable, Hashable {
    let sourcePath: URL
    // otherstuff
}

var files = [URL : Dependency]()

let encoder = JSONEncoder()
let data = try encoder.encode(files.values)

The DependencyList keeps track of all files its seen in a dictionary. However, as each dependency structure also has the relative path of the source file, all that needs to be saved is the values in the dictionary. Unfortunately, this doesn’t work, because as the compiler informs me: Argument type 'Dictionary.Values' does not conform to expected type 'Encodable'.

This was just baffling. What I’d learned in Swift so far is if you get to a point like this it’s because you’re Doing It Wrong™. But it felt wrong to loop through the values and explicitly add them to an array. The eventual solution of converting the values to an array via Array(files.values) similarly feels wrong, but I can’t say why.

Much of how Swift does it’s thing is so deeply technical it borders on magic, but when that magic falls short, it exposes very sharp edges making learning Swift much more challenging. For those of you who started with Swift at 1.0 and have kept pace with all the idiosyncratic changes, you’re accustomed to its quirks. I suspect Swift really is a great learning language if you stick with small lessons and go step by step. But if you’re learning by doing, diving into the deep end, it may be more frustrating than you expect if you’re already a Swift aficionado.


  1. One of the Seattle Xcoders helpfully provided me with a custom operator that would allow me to use ||= instead, but I have strong negative feelings about custom operators left over from my days writing C++. 

  2. Just for comparison, I’ve learned Basic, Pascal, Modula 2, Smalltalk, Lisp, C/C++, 8086 Assembly, Fortran, Cobol, Python, Ruby, Javascript, Objective-C, Java, C#, and now Swift. There are probably some specialised platform languages in there I’ve forgotten. And yes, I know that’s not a huge list. You probably have forgotten more than I’ve learned. 

  3. It was at this point when my complaining was enough to frustrate one Xcoder enough that he mentioned it. I genuinely feel bad about raising his blood pressure and I’m glad we both got away from our computers to enjoy the sunshine. 

The 2019 Spring Party

I’d hoped to have a follow up to the 2018 Spring Party after Molly and I returned from our Summer European adventure, but thanks to lingering construction on our deck, the opportunity slipped away. But it’s now time to think of Spring again and that means it’s time to think of parties. Once again you’re The 2019 Spring Party

Winter Lingers On

Nearly a month ago, I optimistically invited friends to my home to help me welcome Spring. I’d had enough of Winter and was eager for some warmer weather. Because my friends are smartasses, I was careful to set some boundaries: This is not a St. Patrick’s Day party. Do not show up dressed as a Winter Lingers On

Fuck Off Winter Party

I’d like to invite you, yes you, to come celebrate the end of Winter. Officially, Spring doesn’t start until Tuesday, 20 March, but I’m impatient. I’m going to celebrate a few days early on Friday, 16 March9. This is not a St. Patrick’s Day party. Do not show up dressed as a fucking shamrock or Fuck Off Winter Party

Christmas in London

I’ve been a fan of Terry Pratchett ever since I picked up a copy of Small Gods at Munro’s book store10 in Victoria, BC back in the late 90s. That was my introduction to the Disc World and I devoured the few books I was able to find in Seattle book stores. I wasn’t going Christmas in London

Free Workshops in Europe

This Summer I’ll be in Europe from 3 – 15 August and I’d love to host two free workshops for women and other underrepresented groups in our industry. The first workshop teaches how to craft a short presentation (think lightning talk) and the second focuses on presentation skills. If you or someone you know is Free Workshops in Europe

Paypal is the Worst

Recently, I needed to create a Paypal account to receive a reimbursement payment from a conference in Poland. A wire transfer to my credit union wasn’t an option, because my credit union requires an intermediate transfer for international wire transfers (which is just bananas and why I’ve since re-opened my account at Chase). So, my Paypal is the Worst

Winter Silence

A lot has happened since the last time I wrote here. Most importantly, My Favourite Person had an opportunity to perform in her first real show, Chitty Chitty Bang Bang. This was the first time she had to audition for a part. I’m so proud of her for getting a part in the ensemble. She Winter Silence

Not My Favourite

As parents, it’s important not to play favourites with our children. That goes for our furry children too. I don’t know whether my cats can actually understand me when I tell them I love them, but it doesn’t matter. Sometimes, I’ll get a little silly and tell Edison he’s my favourite boy cat, or Madeline Not My Favourite

A Thousand Kisses

Long ago, Molly made an unwise bargain. Her mom had bought her a Furby which Molly wanted to bring over to my house for the weekend. I loathe Furbies and think they should all be destroyed (preferably with cleansing fire), but I was willing to accommodate My Favourite Person: I would allow her to bring A Thousand Kisses