I recently gave a “live” coding tutorial at the Silicon Valley iOS Developers Meetup on how to create your first WatchKit app. By “live” I mean I did it WWDC-style with autocomplete blocks of code when I type in a completion shortcut. For those of you that aren’t familiar with this – you know how Xcode autocompletes things like for-loops and method signatures for you with the necessary pieces so you don’t have to type out the whole thing? It’s the same concept, but using my user-defined snippets instead. I was approached by a few people at the end of my talk wondering how I did this and so I thought I would post a tutorial about it.
Open up Xcode and look at your right sidebar for the snippets section.
There you should see some predefined snippets that Xcode already has installed. You can also add user created snippets by doing the following:
- Highlight the piece of code that you want to turn into a snippet
- Click and drag it over to the snippets window and drop it in
You should now see something like this:
There’s your user created snippet! Right-click on it to get a little preview window.
Click on Edit to change any properties you’d like.
The main things I focused on was changing the title and the completion shortcut. You can also change the completion scope – this is helpful if you’re planning on using the snippet in a scope that’s outside the class implementation.
Warning: I have issues with Xcode (Version 6.3.2 (6D2105)) crashing whenever I try to change the scope the first time. Just reopen Xcode and you should be able to change it. I opened a radar for this so hopefully it gets fixed soon.
That’s it! Go ahead and try out your completion shortcut. If it’s not appearing, then you might need to change the completion scope.
Now we can all do cool WWDC-style live coding 🙂