Speed of Light

Xcode 4 Code Snippets

Perhaps my favourite feature of Xcode 4 is the Code Snippets feature. It allows you to use common bits quickly in your code, instead of requiring you retype them over and over again.

For example, every Objective-C class requires you to provide a dealloc method to clean up any memory you might be hanging on to (of course this is not needed if your subclass hasn't done anything it needs to clean up, but generally you should be releasing or freeing any memory you've asked for in the class if appropriate). One mistake I've often made is the following:

- (void)dealloc {
    [someObject release];

While this looks fine, the problem is I've neglected to invoke the super's implementation of the method (this ought to generate a compiler error, because you compile with -werror, right?). To avoid errors like this, Xcode provides a snippet which demonstrates the proper pattern, has a placeholder for your code, and is available by simply typing “dealloc” and accepting the completion suggestion. It generates the following code for you:

- (void)dealloc {
    (deallocations) // (code) will appear as a blue bubble.
                              // pressing return will accept the bubble as text
    [super dealloc];

Xcode provides a bunch of these Code Snippets, which you can find by opening the Utilities View on the right of your window. Near the bottom, you'll find the Code Snippets Library, in addition to the File Template Library (which works similarly). Snippets can be broken down by platform (Mac OS X, iOS) or viewed all together. Look through the available snippets to see how they might save you from repetitive typing in your code.

The provided snippets are useful, but the cause for my adoration stems from being able to add my own snippets to the library. I've found this to be incredibly useful.

  1. Find some code you'd like to be a snippet and select it.

  2. Drag the selected code to the Snippet Library.

  3. Give it a title and optionally a summary. These are used when browsing through the Snippet Library list.

  4. Choose a completion shortcut to invoke this snippet. My preference is to use the same letter, repeated three times (ie ddd) because it's quick to type, and it's very unlikely to conflict with another completion symbol in my code. Not only does it avoid direct conflicts, but it also lets me filter down the list of suggestions quickly, so I can accept the completion as quickly as possible.

  5. Chose a platform and language, and Completion scope (defaults to the current scope) which lets you choose when the completion shortcut will be available (it wouldn't make sense for your custom initializer method snippet to appear in an Objective-C @interface).

  6. Finally, you may wish to edit the snippet code you've added. The mini-editor is syntax highlighted for you, although unfortunately you can't invoke other snippet completion shortcuts here! Sometimes trying this causes Xcode to have an aneurism.

  7. Bonus: If you'd like your own “blue code bubbles” to appear as part of your snippet, just type the following in your snippet: <#text in the bubble#> When the user invokes the shortcut for the snippet, the inserted text will contain any blue code bubbles you've included in the hash tags. They can be tabbed back and forth (that is, pressing tab will select the next bubble) and can be accepted by pressing Return. You'll see why these are useful in my examples.

Snippets I find useful

Retain/Assign property: Completion shortcuts of rrr and aaa respectively.

@property (nonatomic, retain) <#type#> *<#name#>;
@property (nonatomic, assign) <#type#> <#name#>;

Synthesize for a Custom ivar name: Completion shortcut sss

@synthesize <#property#> = _<#propertyIvar#>;

Useful when you have a custom naming scheme for your instance variables (such as _ivar or mMemberVariable).

Class extension interface: Shortcut eee

@interface <#class name#> ()

Pragma line and name: Shortcut ppp. This could alternatively be on one line. Also, when naming your mark label, write out the symbol name in full and it will be CMD+DoubleClickable, ie UITableViewDelegate methods instead of Table view delegate methods

#pragma mark -
#pragma mark <#Label#>

Define macro: Shortcut ddd. Xcode will autocomplete #define for you out of the box, but I find it slower and it doesn't create tabbable bubbles for you for both arguments. I find adding this snippet to be much more efficient.

#define <#name#> <#substitution#>

While code reuse is an incredibly important factor, there are some bits you just can't help but write over and over again. The Code Snippet library can seriously reduce the hassle of this.