Code Snippet Library

You may have noticed that when you start typing the word init, Xcode will automatically add an init implementation in your source file. If you haven’t 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 utilities area and selecting the Code Snippet Library icon in the library selector (Figure 10.13). Alternatively, you can use the shortcut Command-Control-Option-2, which reveals the utilities area and the Code Snippet Library. Substituting another number in the shortcut selects the corresponding library.

Figure 10.13  Code Snippet Library

Code Snippet Library

Notice that there are a number of code snippets available (Figure 10.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 10.14).

Figure 10.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 can’t edit any of the pre-defined code snippets, but you can create your own. In ItemsViewController.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​ ​[​[​[​P​o​s​s​e​s​s​i​o​n​S​t​o​r​e​ ​d​e​f​a​u​l​t​S​t​o​r​e​]​ ​a​l​l​P​o​s​s​e​s​s​i​o​n​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 10.15 and click Done.

Figure 10.15  Creating a new snippet

Creating a new snippet

Back in ItemsViewController.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. You’ll have to type in that bit of code to get the number of rows yourself. Snippets aren’t magical – just handy.

Make sure to remove the code entered by the snippet, since you have already defined tableView:numberOfRowsInSection: in ItemsViewController.m.

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

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