iOS Presentations/Speaking

Tutorial: WWDC-Style Live Code Snippets

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.
Code snippet library

There you should see some predefined snippets that Xcode already has installed. You can also add user created snippets by doing the following:

  1. Highlight the piece of code that you want to turn into a snippet
  2.  Click and drag it over to the snippets window and drop it in

You should now see something like this:
User-defined code snippet

There’s your user created snippet! Right-click on it to get a little preview window.Code snippet information

Click on Edit to change any properties you’d like.
Code snippet edit view

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 🙂

1 thought on “Tutorial: WWDC-Style Live Code Snippets”

Leave a Reply

Your email address will not be published. Required fields are marked *