Information Technology Reference
In-Depth Information
A code snippet can be a line or two of code such as an alert generator or timer initializer, a more complex sec-
tion of code that implements a standard feature such as animation, or a complete method. Potentially, you can
create snippets that implement an entire class with complex default code.
TIP
Code snippets are built into Code Sense, making a very powerful feature. When you create a new snippet, you can
define a custom auto-completion string. When you type that string in the editor and press Return, Code Sense
copies the snippet code from the library and inserts it. You can add your own placeholder tokens.
Inserting a snippet
To insert a snippet, select the Code Snippet ({}) icon in the Library area of the Utility Pane. Optionally, select an
OS or your custom snippets from the pop-up menu, as shown in Figure 9.7. Select a snippet from the list, and
drag and drop it into the code editor.
The code is copied and added as shown at the top of the figure. Placeholder variables appear in gray. To com-
plete the edit, select and rename them. You can skip between them with the Tab key.
A preview window with the code appears to the left of the snippets. You can use this window to edit snippets in
the library.
CAUTION
By default, the library shows snippets for all platforms. The OS platforms include snippets with different code for
each, but some snippets have common names. Always select the platform before using snippets. Xcode allows you
to add an iOS snippet to an OS X project, and vice versa.
FIGURE 9.7
In this example of adding a code snippet, the tableView: method has been copied from the snippet library and
added to the top of the editor. The floating preview window displays the snippet so you can review it before you
add it.
Search WWH ::




Custom Search