Using XCode 4 Snippets

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
December 6th, 2011 Posted by: (ELC) - posted under:Tutorials

Recently, I came across this post and fell in love with the idea of using XCode 4 snippets. Up until I read the post, I had heard they existed, but never really tried them. After reading the post, I began using his snippets as well as started creating my own. Now, I would say that my work-flow is faster than ever.

Here is a quick demonstration of the power of snippets

Type:

ttt

It generates:

#pragma mark - UITableView Datasource
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 
    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
 
    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row];
 
    return cell;
}
 
#pragma mark - UITableView Delegate methods
 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
 
}

As you can see, with 3 keystrokes, I now have a fully working table view implementation with sample data.

This article will be another introduction to XCode 4 snippets but focusing on how to set them up as well as share them with your team.

Adding A Snippet Inside Of XCode

In my opinion, this is not very obvious. The snippet library is fairly well hidden. To bring it up, first ensure that the right side bar is visible in XCode by selecting the right most option of the “view” pane in the top right corner of XCode.

 

Now that the right bar is available, click on the “{}” option of the bottom most pane. This will bring up the snippet library.

 

Feel free to browse the snippets that XCode ships with, however most of them are not very useful. So, now this is where I feel Apple engineers were a little too clever. It took me a little while to figure out how to actually add a new snippet. You would expect some sort of + button or something. The way you add a new snippet in this case is to write out a chunk of code and drag it into the Code Snippet Library window.

Once you drag the code into the library, it creates a default snippet called “My Code Snippet”. Which again, is just terribly non-obvious. Now, double click on “My Code Snippet”, and then click the “edit” button to modify it.

 

Let’s talk about each of the important fields:

  1. Title – This is just the common name you will use to refer to the snippet. It will display during autocompletion
  2. Completion Shortcut – this is the command you will use to invoke the snippet. For example, I have ttt to automatically create all of the UITableView delegate and datasource methods with sample data.
  3. Completion Scopes – This is pretty cool. You can have the same shortcut to represent differnet snippets based on the scope of the document. For example, you won’t want to invoke your @property snippets inside of your Class implementation. So, they get ignored…

Adding dynamic fields

If you notice, many of Apple’s auto generated classes or snippets (like UIAlertView), have these “code bubbles” hinting at the type of data you should put into them. They are also nice because they allow you to tab between them enabling you to implement the code quicker. To add your own simply insert this into your code:

<#Text#>

Where “Text” is whatever you want the code bubble to say.

That’s it, when you are finished click done and you should now be able to use your snippets.

Sharing Snippets

While XCode doesn’t have an export button for snippets, they are in a fairly predictable location. They are located at:

~/Library/Developer/Xcode/UserData/CodeSnippets/

That being said, you could manually share them between developers (zip them up and email) OR you could version control this folder with something like git. I really like the latter approach. Any time a developer adds a new snippet to the library, the others just have to pull, restart XCode, and voilà the snippet is there.

Conclusion

The only issue now is remembering to actually use your snippets in practice. It takes some getting used to, but I’m sure you will get it. Below, you can download a zip file containing many of the snippets that I use. Simply unzip it into the folder that I mentioned above.

Download My Snippets

Happy iCoding!

Follow me on Twitter

  • Petershine

    This is something I use all the time. Thank you for writing simple instructions for others

  • http://profiles.google.com/pikachuexe Ho Kuen Leung

    The most difficult part is to remember to use those snippets…
    Most of time I remember there is a snippet after typing the code…

  • B

    thanks a lot, you saved me $25.  I was going to purchase keyboard maestro specifically for Xcode templates.  I knew the snippets existed, just didn’t know that I could customize them.

  • http://twitter.com/snx SNake!

    here is a tool to import snippets:
    https://github.com/lukeredpath/xcodesnippets 

    manage all existing created user snippets:
    xcodesnippets migrate 

    install *. codesnippet :
    xcodesnippets install [path-to-snippet-file] 

    and all your snippets will store here: 
    ~/Library/Developer/Xcode/UserData/ManagedCodeSnippets 

  • http://www.cashforsmartphones.com/ Sell Used Cell Phones

    It wouldn’t be that easy if you’re not used in adding snippet but really interesting to try it yourself.

  • Anonymous

    I had just run across snippets myself the other day but hadn’t had a chance to try adding my own. They really did hide the feature, and adding/deleting snippets isn’t exactly intuitive, but once you figure out how – extremely powerful timesaver. Thank you for your snippets and the tutorial!

  • http://twitter.com/hacx Yaniv Danan

    you have a leak in there:
    [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]

    should be autorelease..

  • http://www.easonmusic.com/wiki XMLSDK

    Leak only when not using ARC

  • Jerry

    Thanx, you saved my time and some money ’cause I;m new to coding.
    Jerry from [url=http://www.intellectsoft.net/]mobile application development companies[/url]

blog comments powered by Disqus
canakkale canakkale canakkale balik tutma search canakkale vergi mevzuati