- Built-In Code Snippets
- User-Defined Code Snippets
- Completion Shortcuts / Adding Placeholders / Sharing Code Snippets
Completion Shortcuts
I have found that I rarely, if ever, drag a code snippet into my source code. Instead, I rely on completion shortcuts to add snippets to my code. Completion shortcuts work with Xcode's code completion, which means you only need to type the first few characters of the snippet's completion shortcut to see it in the code completion list.
Developers use different naming conventions for the completion shortcut. Some use no convention at all, while others like to repeat the first character of the completion shortcut to avoid conflicting with other code that might have the same name. For instance, say you have a code snippet for a dispatch_async call. You can assign "dispatch_async" as the completion shortcut for this snippet, but now it appears twice in the code completion list. You can get around this and simplify the typing of the shortcut by setting the completion shortcut to "ddispatch_async". Now code completion will hone in on the code snippet quicker with fewer keystrokes.
Repeating the first character was the approach I used for a long time, but recently I changed my naming convention for completion shortcuts. I now prefix the completion shortcut for all my code snippets with "wps", which is short for White Peak Software. Consistently prefixed shortcuts means Xcode's code completion window groups my code snippets together (see Figure 6). This makes it easier for me to find the right code snippet, especially when I'm looking for a snippet that I don't often use.
Adding Placeholders
Xcode code snippets also support placeholders. A placeholder makes it possible to make changes to the specific parts of the snippet at the time the snippet is added to your source code. A code snippet can have one or more placeholders, and you can tab between the placeholders as you modify the snippet in your source code.
To add a placeholder, include <#name#> in the code snippet where "name" is a string value that represents the placeholder. When the code snippet is used in your code, the placeholder will appear as a blue code bubble (see Figure 7).
Sharing Code Snippets
Code snippets are stored in your ~/Library/Developer/Xcode/UserData/CodeSnippets directory. To share your code snippets, simply copy them from this directory. Each code snippet file is a standalone plist file with a universally unique identifier (UUID) as the file name followed by .codesnippet as the file extension. This file-naming convention eliminates the chance of having conflicting file names when sharing snippets with others.
To help get you started with code snippets, I have shared my collection of snippets via a Github repository. Instructions for “installing” these code snippets are provided in the readme file that is displayed on the repository's home page.
Conclusion
Code snippets are a great way to become more productive with Xcode. If you find yourself writing the same, or similar, piece of code over and over, then it's a good candidate for a user-defined code snippet. I encourage you to give code snippets a try, and remember, anything you type repeatedly into a source code file, be it one line of code or a block of code, can be a code snippet.