Create your own Xcode code snippets

Using the Code Snippet Library from Xcode is very handy sometimes. However, the library consists only of a small amount of available code snippets. But you are not limited to them, rather than you can create your own code snippets for code patterns you are using very often.

Just follow these easy steps below to create your own snippet.

  1. Write your code snippet down at the code editor and select it.
  2. Be sure to open the code snippet library view within Xcode.
  3. Simply drag the selected code to the code snippet library.
  4. Enter a title and a short description of your snippet.
  5. Enter a shortcut for your snippet. You can choose any combination of letters but you should use something meaningful and you can remember easily.
  6. Choose a platform and the corresponding language the code snippet should be available in.
  7. The completion scope is also very handy to determine in which scopes (methods, class implementations, initializers, …) your code snippet should be available.
  8. The last step is to enter some placeholders within your code snippet. These placeholders will be shown as blue bubbles within your editor when using the code snippet and you can easily navigate to them by using your tab key. To create a placeholder just enter <#your_placeholder_name#>.

Example 1:

Creating a new UIViewController  instance from a xib file and push it to the current navigation controller.

<#controller#> *controller = [[<#controller#> alloc] initWithNibName:@"<#controller#>" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:controller animated:YES];
[controller release];

Example 2:

Creating a new UIBarButtonItem and adding it to the current navigation item.

UIBarButtonItem *<#button#> = [[UIBarButtonItem alloc] initWithTitle:<#title#> style:UIBarButtonItemStylePlain target:self action:@selector(<#method#>)];
self.navigationItem.<#left_or_right#>BarButtonItem = <#button#>;
[<#button#> release];

 

One response to “Create your own Xcode code snippets”

  1. Bartolomeo Sorrentino Avatar
    Bartolomeo Sorrentino

    very useful, thanks

Leave a Reply

Your email address will not be published. Required fields are marked *