The Methods Behind the Madness

That's a lot of reading just to understand a single line of code! Thankfully, it gets a little easier with the next two lines, both of which begin with a minus sign (–).

- (NSUInteger)length;
- (unichar)characterAtIndex:(NSUInteger)index;

This is how you define methods (Methods of Class) in a class interface. You can represent any string in Cocoa with just these two methods: The first returns the length of the string; the other returns a Unicode character at an index position.

Tip

When writing about methods in online forums, mailing lists, or blogs, many developers use an abbreviated form for the signatures: They leave out type information and parameter names. Apple's developer documentation also uses this format.

It's common to see something like "I'm having problems with –messageWithFirstParameter:andSecondParameter:" in a post asking for help. Another reason to keep those method signatures readable! The short forms for the NSString methods shown above are –length and –characterAtIndex:. This book uses this format throughout.

The definitions also include types for any parameters and the result. The –length method returns an unsigned integer (NSUInteger). The –characterAtIndex: method takes one parameter with an unsigned integer index and returns a unichar. These methods use primitive types, but you can also specify objects using a pointer to a class name (you'll see that shortly with the –uppercaseString definition).

Tip

The types used in the method definition ...

Get iPhone App Development: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.