Adding custom code snippets extensions

One of the most useful feature of xcode 4 was the built in code snippets library. It allows you to use pre made pieces of code instead of writing them over and over.

You can use those code snippets either by dragging them from the Code Snippet Library section, or by simply typing a completion shortcut.

xcode comes with a variaty of useful code snippets which you are more than welcomed to browse, but today I will show you how to add your own custom code snippets.

Why use code snippets

Code snippets are really great. Code reusability is very important, but a lot of times, it’s just too big of an overhead for the simple piece of code we’re writing. The most obvious example is the for loop. Instead of writing over and over the for line, I just type ‘for’, choose the “For statement” snippet and immediately get the following code

for (<#initialization#>; <#condition#>; <#increment#>) {

The only problem is that the code snippets library that ships with xcode is pretty limited, and every developer usually has his own pieces of code that he uses often. For this reason xcode provide the option to create user-defined snippets

Adding user-defined code snippets

Adding you own code snippets to the library is super easy;

  • Select the piece of code you want to make into a snippet
  • drag the selected code to the Code Snippet Library at the bottom right of the screen.
  • Give it a title, a summary, and a completion shortcut.

If you want to set a place holder for an argument, you can simply write

@property (nonatomic, retain) <#type#> *<#name#>;
The new snippets appear in the “User” section of the Code Snippets Library

And that’s it.

The user generated code snippets are saved in ~/Library/Developer/Xcode/UserData/CodeSnippets
each snippet is a plist file with all the fields you enter.

Examples of useful snippets

There are two major types of code snippets that can come in handy:

Application specific snippets

In most apps, you have specific pieces of code that you use a lot. They can range from a specific NSPredicate format or an Error log message, to setting variables in unit tests.
My convention for such code snippets is to use a completion code built from 3 parts: [AppShortSuffix][ModuleShortSuffix][SnippetShortTitle] For example, Working on Any.DO, all of my unit tests involved adding or altering tasks, so I added a code snippet that adds a task to the DB using the completion code: anytadd, which resulted in the following line:

Task *<#taskName#> = [[StorageManager instance] addNewTaskToStorage:@"<#taskTitle#>"];

Looks trivial, but save a LOT of time.

Global snippets

There are code snippets that are useful to everyone, but that do not appear in xcode’s Code Snippets Library. Some of my favorites are:

// Private declaration

#pragma mark -
#pragma mark Private Declarations
@interface <#ClassName#> (Private)


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

// Praga mark
#pragma mark -
#pragma mark <#Label#>

Since there are so many useful code snippets I decided to start collecting them in a project called:

The Code Snippets Extension Kit

The Code Snippets Extension Kit is an open source project in which I will collect all the interesting code snippets that I write or find.
You can find the project in my Github
Just copy all the snippets to ~/Library/Developer/Xcode/UserData/CodeSnippets/ and you’re set.
You are more than welcomed to contribute to this kit with useful snippets you create.

Where to go from here

Working with custom code snippets take some getting used to, but it can save you an awful lot of time. When you’re coding, try to identify pieces of code, even one line long, that you write over and over, and make a code snippet out of it. If it’s really useful, add it to the extension kit, so that we could enjoy it also 🙂

2 Responses to Adding custom code snippets extensions

  1. PruitIgoe says:

    The one thing that annoys me about the code snippet feature is I can’t just create a snippet from code copied elsewhere. For example, I found a great piece of code on Stackoverflow on creating custom shadow paths. I didn’t need it for my current project but could see where it would be useful for some future project. To store it though I had to cut from SO, paste into my project, drag to the code snippet section in XCode and then delete from my project.

    With admitting this is a “waaaaaa my first world life is not perfect” whine I don’t understand why Apple doesn’t incorporate a button that allows you to create a snippet extension without having to drag code from a project.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: