Guard! Guard!

Swift 2 introduced the guard keyword, a handy bit of syntactic sugar with semantics similar to a debugging assert. It’s pure sugar, nothing more than an alternate if construction, but I’ve found that liberal use of guard has made my code far more readable with very little effort.

Basically, guard is a sort of inverse if. You could just as easily write the (hilariously contrived) above example as:

I strongly prefer the guard version, though, as I find the guard...else line to be much more explicit than the narrow and easily-overlooked !. guard really comes into its own, however, when dealing with optionals. I often find that I end up with a lot of code like:

This is fine, but it feels like inverted logic. I'd far rather take care of the error cases up front, and guard lets me do just that:

UIResponder and Swift 2.0

I was revisiting an older Swift 1.0 project this morning, and discovered that, in the upgrade to Swift 2.0, it had become hilariously uncompilable. The automatic converter worked, frankly, better than I expected, but I was still left with a couple of head-scratching errors. The most confusing was that my SpriteKit subclasses no longer seemed to override their parent UIResponder methods:

No, Xcode, I'm pretty sure it does.

No, Xcode, I’m pretty sure it does.

My touches* methods don’t seem to have the right signature anymore. Turns out, I’d implemented them all as:

Either Swift 1.1 was more relaxed when it came to matching method signatures or the signatures of these methods changed in iOS 9, because the solution turned out to be to change the method signatures to take a set of UITouch objects and an optional event, e.g.:

The App Classifieds

I’ve taken a break from app development to launch something different: a classified listings site for mobile, desktop, and web apps:

The App Classifieds Logomark

If you’re a hobbyist iOS developer with a few spare apps sitting in iTunes Connect (like me), then The App Classifieds can help you turn those apps into something useful by connecting you with like-minded app buyers. There are plenty of markets for apps, sure, but most of them are either borderline scammy, auction-only, or too expensive to make sense for smaller apps. The App Classifieds is a place to list apps that you’d be willing to sell but aren’t necessarily looking to auction right now — and listing an app is totally free. Promoted listings are also available, if you want to maximise the number of potential buyers who see your app — use the code SWIFTALICIOUS to get promoted listings for just $1.25/month (50% off of $5/month).

I wrote a little more about why we need a better way market for apps over at The App Classifieds blog; check it out for a better introduction. And if you’ve never sold an app or taken over someone else’s, I’d give it a shot. You’ll be surprised!

Swift 1.2, or The End of Egypt.

Swift 1.2 is out! Well, sorta-out — it’s bundled with the latest Xcode beta, and I’m still a little hesitant to jump on the beta Xcode train having been burnt one time too many. Plus, I’m not sure I like the idea of having my dev tools be ‘beta’, especially in the oh-wow-it-really-is-beta way that Apple uses the term.

If you want a highlight of changes in 1.2, jump over to NSHipster — but the most exciting change for me is a slight syntactic sugar around optional binding that turns the let-pyramid of doom:

into a sensible, manageable one-liner:

UIAlertController or Bust

You know what I never liked? UIAlertView. You know what’s been deprecated in iOS 8?

That’s right.


It (along with UIActionSheet) has been replaced by UIAlertController. There’s a great full rundown of the differences between old-style alerts and the new UIAlertController over at NSHipster, but below is a quick example of how to show a simple confirmation alert:

I especially love the flexibility of UIAlertActionStyle, which allows you to specify whether an action is a cancellation or is in some way destructive.

Not an Objective-C Replacement…

Comparison of Modern C++ and Apple’s Swift Programming Language Syntax

Most modern 3rd-generation languages have many similarities, it just takes getting use to the small differences in order to be able to code between them.

The more Swift I write the more I’ve realised: Swift isn’t a modern take on Objective-C — rather, it’s Apple’s take on C++. This time last year I couldn’t imagine using an Apple-only replacement for C++14; now I can’t imagine writing for iOS in anything else.

#pragma mark Equivalent in XCode 6

Remember #pragma mark? In Objective-C code it’s a lovely way to make your methods a little easier to find in the Xcode source navigator. While Swift doesn’t have Objective-C’s preprocessor support (and good riddance) I’ve come across a few magic comments that, if conservatively used, can make your code a lot more readable: MARK, TODO, and FIXME.

The effect of MARK, TODO, and FIXME in Xcode 6

The effect of MARK, TODO, and FIXME in Xcode 6


Use // MARK: Section Description to break your code up into logical sections. I use MARK breaks to (among other things) group together methods that implement a particular interface or share some related function (e.g. touchesBegan, touchesMoved, &c.). Start your section description (the bit after MARK: with a single dash to set the code off with a separator.


Use // TODO: Message to your future self! to indicate places in your code that still need work.


As near as I can tell, FIXME works exactly like TODO. Take your pick, I guess — but try and stay consistent. I exclusively use TODO, but that’s just a personal preference.

Introducing alt.hack

For the last few months I’ve been working on a new game using SpriteKit and (of course) Swift. In a past life I used to be a game developer (tip o’ the hat to Blazing Griffin, the studio I co-founded back in 2011), and it’s been so great to get back into building games. Without further ado, let me introduce alt.hack, a hacking game for iOS and OS X loosely based on the hacking mechanic in Deus Ex: Human Revolution.

alt.hack, a hacking game coming to iOS

alt.hack, a hacking game coming to iOS

In alt.hack you play as an aspiring hacker, breaking your way through a series of increasingly well-defended networks. Every hack is a race against time as you capture your way across a network before security can trace you and kick you out. The game has a lengthy story mode, plus a fully-featured level designer (so fully-featured that I’ve used it to create every level in the game!) that lets you publish networks and see how other players fare against them.

I started to document the things I was learning about Swift as I build alt.hack, and I’ll keep doing that as it moves closer to release. You might have noticed that a few of the more popular posts here (e.g. How to Setup a 2D Camera in SpriteKit) have a very game-specific focus — now you know why!