Back story: my period of walkabout, in which I went to see the rest of the computing world beyond Apple land, started in November 2014. This was shortly after Swift’s introduction at WWDC 2014. It ended in October 2018, by which time the language had evolved considerably, its position in the community had advanced greatly, and SourceKitService had stopped crashing.
I have previously written on the learning phases I encountered on exposure to Haskell, now what about Swift? I have the opportunity to reflect on how I react as a beginner, and share that so that we all learn how we (well, I) learn, and maybe discover how we can teach.
About the project
I’m writing a tool that I want, which takes files in one format (RSS) and writes them out in another format (Maildir). You can follow along. The reason for mentioning this here are twofold:
- I do not know what I’m doing, but I’m willing to share that.
- To let you understand the (limited, I think) complexity of the thing I’m trying to build.
Thinks: This should not be that hard
I often feel like Swift is making me feel like an idiot. This is because my expectation is too high: I know the platform fairly well. I know the Foundation framework pretty well. I know Xcode pretty well. I understand my problem to some extent. It should just be the programming language that’s different.
And I do different programming languages all the time. What’s another one going to do?
But of course it’s not just the programming language that changed. It changed the conventions for things like naming methods or raising errors, and that means that the framework methods have changed, which means that things I used to know have changed, which means that I do not know as much as I assume. It introduced a new library, which I also don’t know.
Thinks: That unimportant thing was really frustrating
Two such convention changes are correlated: classes that used to be Foundation and are now standard library (or maybe are Foundation still but have been renamed on being bridged, I’m not sure) are renamed from NSThing
to Thing
. That means that the name of NSURL
is now URL
.
That means that if you have a variable that represents a URL, you can’t follow the Cocoa convention of leaving the abbreviation uppercased and calling it URL
, because now it’s got the same name as the type URL
. So the new convention is to call it url
.
Objectively, that’s not a big deal. Subjectively, this stuff is baked in pretty deep, and changing it is hard.
Thinks: Even learning something is frustrating
The last event to make me get up and walk around a field was actually discovering something new about Swift, which should be the point, but nonetheless made me feel bad.
I have discovered that when it comes to working with optionals, the language syntax means that There Is More Than One Way To Do It. When I learned about if let
and guard let
, I was confused by the fact that the thing on the right needed to be an optional, not unwrap one: surely if my rvalue is an optional, then my lvalue should be, too?
Then, when I learned about the ?.
and subsequently ??
operators, I thought “there’s no way I would ever have thought to type that, or known how to search for those things”. And even though they only make things shorter, not different, I still felt frustration at the fact that I’d gone through typing things out the long way.
Thinks: There’s More Than One Way Not To Do It
One of the Broken Expectations™ is that I know how to use Strings. Way back when, NeXT apps used char *
as their string type. Then Enterprise Objects Framework came along with its Foundation
library of data types, including a new-fangled Unicode string class, NSString
. Then, well, that was it for absolute ages.
So, when I had a String
and I wanted to take the substring to an index, I was familiar with -substringToIndex:
and tried to apply that. That method is deprecated, so I didn’t want to use it. OK, well I can string[0..<N]
. Apparently not, integer subscripting is not allowed, and the error message tells me to read a code comment to understand why. I wish it told me where that code comment was, or just showed it to me, instead!
Eventually I found that there’s a .prefix(N)
method, again this is the sort of thing that makes me think: what’s wrong with me? I’ve been programming for years, I’ve been programming on this platform for years, I should be able to get this.
Conclusion: Read a Book
I had expected that my knowledge of the Mac, Xcode, and Cocoa would be sufficient to carry me through a four-year gap on picking up a language, particularly with the occasional observation of a conference talk about the Swift language (I’ve even given one!). I had expected that picking up a project to build a thing would give me a chance to get acquainted.
I was reflecting on my early experiences with writing NeXT and Mac applications in Objective-C. I had my copy of the NeXT Developer Documentation, or Cocoa in a Nutshell, open on the desk, looking at the methods available and thinking “I have this, I want that, can I find one of these that gets me there?” I had expected that auto-complete in Xcode would be my modern equivalent of working that way.
Evidently not. Picking up the new standard library things, and the new operators, will require deliberate learning. I’ve got some videos lined up, but I think my next action is to find a good book to read.
I’m enjoying Swift Style, by Erica Sadun.
She does a great job explaining not only how things work, but the preferred ways of expressing it.
Thanks for the recommendation!