A Weekend Lost to Swift Playgrounds

A Weekend Lost to Swift Playgrounds

Tutorials ios-development objective-c swift wwdc xcode

So WWDC was last week, and like apparently every other iOS developer with a blog, I downloaded the Xcode 6 beta the second I could and spent most of Saturday and half of Sunday poking at Swift. Im not going to do the whole "here is what Apple announced" post because if you havent read six of those already this week you havent been on the internet. What I want to write about instead is what its actually like to sit down and write code in this thing, because that part has been weirdly underdiscussed.

First, the practical stuff. You need a paid Apple Developer account ($99/year, the same one youve probably already got if youve shipped anything to the App Store) to even get the Xcode 6 beta off the dev center. It downloaded slow for me both days, presumably because half the planet was hitting Apples servers at once. Once its in, the playgrounds feature is the thing everyone should try first, not the compiler. You type code in a left pane and it shows you live results in a sidebar as you go, no build-and-run cycle at all. I made a dumb little playground that just looped through Fibonacci numbers and watched the results update line by line as I edited the loop bounds. It felt like using a graphing calculator instead of writing software, in a good way.

The language itself is a mixed bag for me so far, and Ill just say my actual opinion instead of hedging: I like it more than Objective-C already, and I say that as someone who defended Objective-C's brackets to a coworker for like forty minutes last year. Not having to write [NSString stringWithFormat:@"%@", thing] when you can just write "\(thing)" is genuinely nice. Type inference means you skip a lot of the boilerplate that made Objective-C reads feel like reading a legal document. Semicolons are optional and I turned them off almost immediately and havent missed them once.

Optionals are the part that ate my Sunday. If you havent touched Swift yet, the short version is that any variable that might not have a value has to be declared as optional with a ?, and then you have to unwrap it before you use it, either by force-unwrapping with ! (which will crash your app if its actually nil, so dont get lazy) or by checking it safely with if let. Conceptually I get why this exists, it's supposed to kill the whole category of nil-pointer crashes that Objective-C just shrugs at. But my first hour with it was just fighting the compiler over unwrapping chains, thing?.property?.otherProperty, question marks everywhere like the code was nervous about something. By Sunday afternoon it clicked and I stopped fighting it, but that learning curve is real and I dont think the WWDC session video did a good enough job warning people.

Also, and this is a small complaint but it annoyed me for like twenty minutes: the beta crashed on me twice just running a playground with a recursive function in it, no error message, just gone. Its a first beta, fine, I get it, but write your test apps in something you can afford to lose for a while.

One thing I didnt expect: Apple put out a free 500-page-ish iBook called The Swift Programming Language the same day as the keynote, and its actually a solid reference, way better written than most Apple documentation I've had to slog through over the years. I read the first three chapters on the actual iPad in bed Saturday night instead of doing anything productive, which tells you something about how the writing holds up.

Im not rewriting anything in production with this yet, obviously, its a first beta and the syntax is apparently still shifting release to release based on some threads Ive seen. But if youve got a free weekend and an Apple ID with $99 already sunk into it, pull down Xcode 6 and just mess around in a playground for an hour. You dont need a real project. You dont even need to finish anything. Just watch the sidebar update while you type and see if it grabs you the way it grabbed me.

Next week Ill probably try porting one of my smaller side project view controllers over and see how ugly the interop with existing Objective-C code gets, since apparently you can mix both in one project. That sounds like it could go fine or could go very badly. Well see.