- Built-In Code Snippets
- User-Defined Code Snippets
- Completion Shortcuts / Adding Placeholders / Sharing Code Snippets
User-Defined Code Snippets
Having a collection of pre-defined code snippets is handy, but code snippets become much more useful when you make your own. User-defined code snippets are marked in the library with the word "User." You can filter the library list to show only user-defined snippets by selecting "User" in the library's dropdown list (see Figure 5), and you can also filter by platform: iOS and Mac.
To create a user-defined code snippet:
- Highlight the piece of code in the editor the represents the code snippet.
- Drag and drop the highlighted code into the Code Snippet library. This will display the code snippet information popup window.
- Set the title for the code snippet. You can also give it a short summary that explains the snippet and its purpose.
- Set the completion shortcut to some string value that is used to add the snippet into your code without dragging.
- Click the Done button to save the snippet and close the window.
You can also specify one or more platforms and completion scopes for the code snippet. A platform is used to filter the code snippets shown in the library list. A completion scope is used by Xcode's code completion to filter out code snippets that don't make sense based on the editor's current cursor location within the source code.
An example of completion scope in action is my code snippets for the method -initWithDefaultNib. Oftentimes I add this method as the designated initializer for my view controllers. One snippet defines the method's declaration statement that's included in @interface section of the class, and the other snippet defines the method's implementation stub that's found in the @implement section. The first snippet has a completion scope of Class Interface Methods, and the completion scope for the second is Class Implementation.
To delete a user-defined code snippet:
- Select the snippet in the Code Snippet library list.
- Press the Delete key, and confirm the delete request.
To edit a user-defined code snippet:
- Click a snippet in the Code Snippet library list. After a brief moment of waiting, the code snippet information popup window will display.
- Click the Edit button to alternate between edit and read-only modes for the selected code snippet.
- Click the Done button to close the information window.
While the code snippet information window is open, you can select another code snippet in the library list to see its details. This is easier and less annoying then clicking the Done button followed by clicking another snippet each time you need to review or edit more than one snippet.