Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book

Using Code Snippets

When you are writing code, you will often find yourself typing statements that are very similar to one another: catch-try blocks, if-then statements, switch statements, and so on. Instead of having to remember the syntax of each of these common code sequences, you can make use of the built-in Xcode Code Snippet Library.

You can access the Code Snippet Library from the Xcode Source Editor by choosing View, Utilities, Show Code Snippet Library, or by pressing Control+Option+Command+2. The library appears in the Utility area and is represented by the { } icon, as shown in Figure 6.23.

Viewing Snippets

To view what a snippet contains, click its entry in the library. A popover window appears displaying the contents of the snippet, as shown in Figure 6.24.

Notice that certain parts of the snippet are highlighted in gray. These represent portions of the code that you need to provide. When a snippet is inserted into your code, you can use the Tab key to jump between these areas. Click the Done button when viewing the snippet code to hide the popover.

FIGURE 6.23

FIGURE 6.23 The Code Snippet Library contains snippets of useful code.

FIGURE 6.24

FIGURE 6.24 View the contents of a snippet.

Inserting Snippets

To insert a code snippet into your class files, just drag and drop from the Code Snippet Library into your code. Wherever your cursor points, the snippet is inserted, exactly as if you had typed the code yourself.

Adding Snippets

Not only can you use the existing snippets to help code, but when you write useful code sequences of your own, you can add them to the Code Snippet Library as user-defined snippets. To do this, follow these steps:

  1. Select the code in the Source Editor that you want to turn into a snippet.
  2. Make sure the Code Snippet Library is visible (Control+Option+Command+2).
  3. Click and drag from the selected text to the Code Snippet Library, as shown in Figure 6.25.
FIGURE 6.25

FIGURE 6.25 Drag the code to the Code Snippet Library.

The text is added to the library under the title My Code Snippet, as shown in Figure 6.26.

FIGURE 6.26

FIGURE 6.26 The new snippet is visible in the library.

Because the default name is not very descriptive, you will probably want to edit it to reflect its purpose.

Editing Snippets

To edit a snippet you have defined, follow these steps:

  1. Click it in the library so that the popover appears displaying its code.
  2. Click the Edit button in the lower-left corner. Figure 6.27 shows a snippet in edit mode.
    FIGURE 6.27

    FIGURE 6.27 Edit the snippet and the information stored about the snippet.

  3. Use the fields in the Source Editor to set a title and summary for the snippet and define the platform and development language the snippet applies to.
  4. Enter a shortcut that can be typed to automatically insert the snippet and the different areas (scopes) within a typical source file where the completion shortcut should be active.
  5. Click Done to save the snippet.
  • + Share This
  • 🔖 Save To Your Account