Class references

So where do you find methods that you need? Apple maintains a class reference for each class in its APIs. The class reference lists all of the methods of a class and basic information on how to use them.

In Xcode, select HelpDocumentation and API Reference. This will open Xcode’s documentation browser.

In the search field at the top of the window, enter NSString.

Figure 16.1  NSString class reference

NSString class reference

(You can also access the documentation via Apple’s developer website. To get to the NSString class reference, simply search for NSString class reference. The first result returned is usually the NSString reference page at developer.apple.com.)

In the righthand pane is the table of contents for the NSString class reference. The Overview describes the NSString class in general. There are also headings that list the class methods and instance methods. If you know the name of the method you are looking for, then you can find it by name under one of these headings and read all about its details.

Reveal the contents of the Class Methods category. Find and select stringWithFormat: from the list to see useful information about this method, like descriptions of its parameters and return value.

Figure 16.2  Documentation for stringWithFormat:

Documentation for stringWithFormat:

If you need to do something with an object but do not know of a specific method, then the best place to start is the Tasks heading. One task that developers often need to accomplish with NSString is searching one string to see if it contains a certain substring. A substring is a string that may make up part or all of another string.

For instance, say you read in a comma-delimited list of names as an NSString object. Now you need to check if a particular name is in the list. That single name would be a substring of the larger string.

Reveal the contents under the Tasks heading. Find and select Finding Characters and Substrings. This will reveal several potentially useful methods.

Figure 16.3  Methods for finding characters and substrings

Methods for finding characters and substrings

In the real world, you would browse through the details of candidate methods until you found one that would work. For this example, we will give you a head start: click rangeOfString: in the list of methods to see its details (Figure 16.4).

Figure 16.4  Documentation for rangeOfString:

Documentation for rangeOfString:

You can see that rangeOfString: has one parameter that is an instance of NSString. This is the substring for which you want to search – the single name to find in the list of names.

You can also see that this method returns an NSRange. What is an NSRange? Click NSRange to view its definition (Figure 16.5).

Figure 16.5  Documentation for NSRange

Documentation for NSRange

NSRange is a typedef for a struct, like you used in Chapter 11. It has two members, location and length, that you can use to pinpoint a substring within a string.

However, in the current problem, you only want to see if the name occurs in the list or not. To figure out how to do this, press the back button at the top lefthand corner of the documentation browser to return to the previous page. Then find the Return Value section in the rangeOfString: documentation. This section states that when the passed-in substring does not occur, rangeOfString: returns an NSRange whose location is the constant NSNotFound.

Thus, to determine whether the name is in the list of names, you can simply check the return value’s location member. The code would look something like this:

N​S​S​t​r​i​n​g​ ​l​i​s​t​O​f​N​a​m​e​s​ ​=​ ​@​"​.​.​.​"​;​ ​/​/​ ​a​ ​l​o​n​g​ ​l​i​s​t​ ​o​f​ ​n​a​m​e​s​
N​S​S​t​r​i​n​g​ ​n​a​m​e​ ​=​ ​@​"​W​a​r​d​"​;​
N​S​R​a​n​g​e​ ​m​a​t​c​h​ ​=​ ​[​l​i​s​t​O​f​N​a​m​e​s​ ​r​a​n​g​e​O​f​S​t​r​i​n​g​:​n​a​m​e​]​;​
i​f​ ​(​m​a​t​c​h​.​l​o​c​a​t​i​o​n​ ​=​=​ ​N​S​N​o​t​F​o​u​n​d​)​ ​{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​N​o​ ​m​a​t​c​h​ ​f​o​u​n​d​!​"​)​;​
 ​ ​ ​ ​/​/​ ​O​t​h​e​r​ ​a​c​t​i​o​n​s​ ​t​o​ ​b​e​ ​t​a​k​e​n​
}​ ​e​l​s​e​ ​{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​M​a​t​c​h​ ​f​o​u​n​d​!​"​)​;​
 ​ ​ ​ ​/​/​ ​O​t​h​e​r​ ​a​c​t​i​o​n​s​ ​t​o​ ​b​e​ ​t​a​k​e​n​
}​
..................Content has been hidden....................

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