http://nshipster.com/xcode-snippets/


iOS development all but requires the use of Xcode. To its credit, Xcode has improved pretty consistently over the last couple of years. Sure, it still has its… quirks, but hey—things could be much, much worse.

Working in an IDE may not be as cool as working in your favorite decades-old editor (or that other one), but you know what is cool? Autocompletion. Not to mention Build & AnalyzeBreakpoints, andInstruments.

This is all to say: if we’re resigned to use an IDE in our development workflow, we might as well make the most of it, right? So this week on NSHipster, we’re going to talk about one of the more powerful yet underused features of Xcode: Code Snippets.


From @interface declarations to if (!self) return nil;

Using Xcode Snippets

To see the available code snippets, show the Utilities panel, to the right of your editor. On the bottom half the Utilities panel, there will be a horizontal divider with 4 icons.

Click the { }

There are two ways to insert a snippet into your code:

You can drag and drop from the code snippets library into your editor:

…or for snippets that include a text completion shortcut, you can start typing that:

To get a sense of what you can do with snippets, here’s an overview of the ones built-in to Xcode:

typedef

  •  declarations for 

enum

structunion

  • , and blocks
  • C control flow statements like 

if

if

else

  • , and 

switch

  • C loops, such as 

for

while

  • , and 

do

while

  • C inline block variable declaration
  • Objective-C declarations for 

@interface

  •  (including for class extensions and categories),

@implementation

@protocol

  • Objective-C boilerplate for KVO, including the relatively obscure

keyPathsForValuesAffecting<Key>

  • , used for registering dependent keys
  • Objective-C boilerplate for Core Data fetches, property accessors, and property validation
  • Objective-C idioms for enumerating 

NSIndexSet

  • Objective-C incantation for 

init

initWithCoder:

  •  and 

initWithFrame:

  • Objective-C 

@try

  •  / 

@catch

  •  / 

@finally

  •  and 

@autorelease

  • GCD idioms for 

dispatch_once

  •  and 

dispatch_after

Creating Xcode Snippets

Of course, what really makes snippets such a powerful feature is the ability to create your own.

The process of creating a snippet is actually pretty unintuitive and difficult to explain. It uses an obscure OS X system feature that allows users to create a “Text Clipping” by dragging and dropping selected text. Much easier to just show it in action:

After being added to the code snippet library, a user-defined snippet can be edited by double-clicking its listing:

Each snippet has the following fields:

  • Title
  • Summary
  • Platform
  • Language
  • Completion Shortcut
  • Completion Scopes - Limits the snippet visibility for text completion to the specified scopes. For example, an 

if

  •  / 

else

  •  statement should only be auto-completed from within a method or function implementation. Any combination of the following:
  • All
  • Class Implementation
  • Class Interface Methods
  • Class Interface Variables
  • Code Expression
  • Function or Method
  • Preprocessor Directive
  • String or Comment
  • Top Level

Each Xcode snippet has a file representation in~/Library/Developer/Xcode/UserData/CodeSnippets/

Placeholder Tokens

Something you may have noticed in using other Xcode snippets are placeholder tokens:

In Xcode, placeholder tokens are delimited by <# and #>, with the placeholder text in the middle. Go ahead—try typing that into Xcode, and watch as the text between the octothorp tags magically transforms right in front of your eyes.

Include placeholder tags to add a dash of dynamism in your own snippets!

Third-Party Xcode Snippets

A list of generally useful code snippets can be found in this GitHub project (pull requests welcome!). If nothing else, this also serves as an example of what’s possible.


Programming isn’t about being an expert typist, so don’t make it any more difficult for yourself than it needs to be. If you find yourself groaning while typing some inane, rote-memorized code idiom, take a minute to create a snippet for it instead!


nsmutablehipster

Questions? Corrections? Issues and pull requests are always welcome — NSHipster is made better by readers like you.

Find status information for all articles on the status page.