Code Snippet Library

You may have noticed that when you start typing the method definition for init in an implementation file, Xcode will automatically add an init implementation in your source file. If you have not noticed this, go ahead and type init in an implementation file and wait for the code-completion to kick in.

The freebie code comes from the code snippet library. You can see the code snippet library by opening the utility area and selecting the Code Snippet Library tab in the library selector (Figure 8.13). Alternatively, you can use the shortcut Command-Control-Option-2, which reveals the utility area and the Code Snippet Library. Substituting another number in the shortcut selects the corresponding library.

Figure 8.13  Code snippet library

Code snippet library

Notice that there are a number of code snippets available (Figure 8.13). Click on one and, in a moment, a window will appear with the details for that snippet. Click the Edit button on the code snippet detail window (Figure 8.14).

Figure 8.14  Snippet editing window

Snippet editing window

The Completion Shortcut field in the edit window shows you what to type in a source file to have Xcode add the snippet. This window also tells you that this snippet can be used in an Objective-C file as long as you are in the scope of a class implementation.

You cannot edit any of the pre-defined code snippets, but you can create your own. In BNRItemsViewController.m, locate the implementation of tableView:numberOfRowsInSection:. Highlight the entire method:

-​ ​(​N​S​I​n​t​e​g​e​r​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​a​b​l​e​V​i​e​w​
 ​n​u​m​b​e​r​O​f​R​o​w​s​I​n​S​e​c​t​i​o​n​:​(​N​S​I​n​t​e​g​e​r​)​s​e​c​t​i​o​n​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​[​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​a​l​l​I​t​e​m​s​]​ ​c​o​u​n​t​]​;​
}​

Drag this highlighted code into the code snippet library. The edit window will appear again, allowing you to fill out the details for this snippet.

One issue with this snippet is that the return statement is really specific to this application – it would be much more useful if the value returned was a code completion placeholder that you could fill in easily. In the edit window, modify the code snippet so it looks like this:

-​ ​(​N​S​I​n​t​e​g​e​r​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​a​b​l​e​V​i​e​w​
 ​n​u​m​b​e​r​O​f​R​o​w​s​I​n​S​e​c​t​i​o​n​:​(​N​S​I​n​t​e​g​e​r​)​s​e​c​t​i​o​n​
{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​<​#​n​u​m​b​e​r​ ​o​f​ ​r​o​w​s​#​>​;​
}​

Then fill out the rest of the fields in the edit window as shown in Figure 8.15 and click Done.

Figure 8.15  Creating a new snippet

Creating a new snippet

In BNRItemsViewController.m, start typing tablerows. Xcode will recommend this code snippet and pressing the return key will automatically complete it for you – and the number of rows placeholder will be selected. (If there are several placeholders, Control-/ will hop you to the next.)

Before continuing, make sure to remove the code entered by the snippet because you have already defined tableView:numberOfRowsInSection: in BNRItemsViewController.m.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset