First Weekend With Swift Playgrounds

First Weekend With Swift Playgrounds

Tutorials ios-development swift wwdc xcode

So I did basically nothing productive this weekend except mess around with Swift.

If you somehow missed it, Apple dropped a brand new programming language at WWDC on Monday, completely out of nowhere. No leaks, no rumors, nothing — one minute Craig Federighi is talking about app extensions and the next he's up there saying "we have a new programming language" like its no big deal. Ive been writing Objective-C since 2010 and my first reaction watching the stream (at like 1am here, because timezones are the worst) was something between excitement and betrayal. Four years of memorizing bracket syntax and now theres this.

Anyway, Xcode 6 beta is free to grab if youre a registered developer, no $99 program required for the beta itself, so I threw it on my machine Tuesday night and finally had a real block of time to sit with it this weekend.

The Playgrounds thing is the actual selling point

Everyone's going to talk about syntax (fair, its way less verbose, no more semicolons, no header files for basic types) but the thing that actually made me sit up was Playgrounds. Its this split-pane editor where you write Swift on the left and get live results rendered on the right, updating as you type. No build-and-run cycle. No waiting for the simulator to boot up for the ninth time in an hour just to check if a loop is doing what you think its doing.

I threw together a dumb little playground that just looped through drawing circles with slightly different radii and watched the results populate in the sidebar in real time. Its the kind of thing thats hard to explain in text and genuinely worth just trying yourself if you have five minutes and Xcode 6 beta installed.

A few things that tripped me up right away, for anyone else poking at this:

  • Optionals. Everything with a ? after the type means it might be nil, and you have to unwrap it before using it, either with ! (if youre sure, and willing to crash if youre wrong) or with if let. Coming from Objective-C where sending a message to nil just silently does nothing, this felt annoying at first and now I kind of get why its there. Its going to save people from a whole category of bugs, eventually, once everyone stops just slapping ! on everything out of impatience. I caught myself doing exactly that about ten minutes in.
  • Type inference is aggressive. let name = "Ramith" just knows its a String, you dont declare it. Took some getting used to after years of typing out NSString *.
  • println() instead of NSLog, and it doesn't need a format string for basic stuff. Small thing but it made me smile.

The rough edges

Its a beta, so obviously things are rough, but Im noting a few here mostly so I remember later how bad it was at launch. Autocomplete is slow to the point of being genuinely useless some of the time — I'd type a dot after a variable and just wait, staring at a spinning nothing, for two or three seconds. My fan was audibly working overtime just from having a playground open with maybe 40 lines of code in it, which is a little funny given the name. Also bridging Swift with existing Objective-C code (which basically everyone is going to need to do, since no ones rewriting an entire app from scratch) requires this bridging header setup thats not super well documented yet.

Apple also quietly published a free 500-plus page book, "The Swift Programming Language," straight to iBooks the same day as the keynote. I skimmed maybe sixty pages of it on the train yesterday. Its actually well written for an official language reference, which in my experience is rare, most of these docs read like they were assembled by three different teams that never talked to each other. This one at least reads like a single person had a coherent point of view about the language.

Im not going to pretend I know yet whether Swift replaces Objective-C for real production apps anytime soon. Probably not this year, not with a 1.0 that isnt even out yet and an entire industry of existing codebases sitting in Objective-C. But as a weekend toy its been the most fun Ive had with a new dev tool in a long while, and thats worth saying even if the beta crashed on me twice while I was writing this post.