Using Code Snippets in Xcode
I am really looking forward to the new Objective-C literal syntax being added to Xcode 4.4. Sadly, I am stuck on Xcode 4.3 for at least a little while longer. So, I decided to look into using code snippets to try and mimic at least some of the new Objective-C literals.
The first thing we can do is add our own support for
@NO. To do this, launch Xcode and ensure that the code snippet library is visible by selecting
View -> Utilities -> Show Code Snippet Library and then filtering the list down to your user snippets by selecting “User” from the dropdown. Now, in your code editor, type
[NSNumber numberWithBool:YES], select the text, and drag it over to the code snippet library. This will create your first code snippet that should look like the following:
As it stands, this snippet won’t really do anything. We need to assign a completion shortcut to it, and while we do that we might as well edit it and clean it up a little. Hit the edit button and set the title to
[NSNumber numberWithBool:YES], the completion shortcut to
@YES, and the completion scope to “Code Expression”. Your snippet should now look like the following:
Now it is time to save your code snippet and try it out! All you have to do to type
[NSNumber numberWithBool:YES] is to type
@YES followed by
<return> to accept the suggested completion. This is not quite as good as native support for Objective-C literals, but it saves about the same amount of typing.
You can repeat the above process for
@NO, but I won’t go over that here. Instead, let’s try and make it easier to create NSNumbers that hold ints, doubles, and floats.
Getting this to work is going to require a more-complicated code snippet as the snippet will need to support a single parameter. First, let’s create a code snippet for NSNumber’s
numberWithInt: method by typing
[NSNumber numberWithInt:<#(int)#>] into our code editor and dragging that text to the code snippet library. The
<#(int)#> construct tells Xcode that this snippet takes a single parameter and to display
(int) as a hint to the user.
Change the new snippet’s title to
[NSNumber numberWithInt:], its completion shortcut to
@int, and its completion scope to “Code Expression”. Now, type
@int into your code editor and hit
<return>. You should see
[NSNumber numberWithInt:(int)] appear in your editor with
(int) already selected so that you can immediately start typing the value of your integer. Now you can repeat the same process for
@float. It’s not quite the same as the Objective-C literals, but it should tide us over until Xcode 4.4 is released.
If you want to take these snippets one step further, you could complete
[NSArray arrayWithObjects:<#(id), ...#>, nil] and
[NSDictionary dictionaryWithObjectsAndKeys:<#(id), ...#>, nil]. However, it’s a pain to create all of these code snippets one after the other. Wouldn’t it be nice to be able to just drag them all into Xcode from a library of useful code snippets? Well you can!
Xcode stores all user-defined code snippets in
~/Library/Developer/Xcode/UserData/CodeSnippets. By default, each snippet is named using a universally unique identifier. However, it appears that you can rename the code snippet files to whatever you want and Xcode will still load them. This makes it much easier to manage your snippets by giving them descriptive names and storing them in a Git repository somewhere.
All of my code snippets (including the ones in this post) can be found on GitHub. To clone the repository directly to the appropriate directory, run:
Or, if you’d rather, just copy all of the
.codesnippet files inside the repository directly to your
I hope these code snippets will make your lives a little easier until Xcode 4.4 comes out. If you have any other snippets you find invaluable, let me know on Twitter!