More on NSArray and NSMutableArray

You will frequently use arrays when developing iOS applications, so let’s go over some more array-related details.

An Objective-C array can contain objects of different types. For example, although your items array currently only contains instances of BNRItem, you could add an instance of NSDate or any other Objective-C object. This is different from most strongly typed languages where an array can only hold objects of a single type.

Objective-C arrays can only hold references to Objective-C objects. Primitives and C structures cannot be added to an Objective-C array. If you need to add primitives or C structures, you can wrap them in Objective-C objects written for this purpose, including NSNumber, NSValue, and NSData.

Note that you cannot add nil to an array. If you need to add holes to an array, you must use NSNull. NSNull is a class whose only instance is meant to stand in for nil and is used specifically for this task.

 ​ ​ ​ ​[​i​t​e​m​s​ ​a​d​d​O​b​j​e​c​t​:​[​N​S​N​u​l​l​ ​n​u​l​l​]​]​;​

When accessing members of an array, you have used the objectAtIndex: message with the index of the object you want returned. This, like many other elements of Objective-C, is very verbose. Thus, there exists a shorthand syntax for accessing members of an array:

N​S​S​t​r​i​n​g​ ​*​f​o​o​ ​=​ ​i​t​e​m​s​[​0​]​;​

This line of code is equivalent to sending objectAtIndex: to items.

N​S​S​t​r​i​n​g​ ​*​f​o​o​ ​=​ ​[​i​t​e​m​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​0​]​;​

In BNRItem.m, update randomItem to use this syntax when creating the random name.

+​ ​(​i​n​s​t​a​n​c​e​t​y​p​e​)​r​a​n​d​o​m​I​t​e​m​
{​
 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​r​a​n​d​o​m​N​a​m​e​ ​=​ ​[​N​S​S​t​r​i​n​g​ ​s​t​r​i​n​g​W​i​t​h​F​o​r​m​a​t​:​@​"​%​@​ ​%​@​"​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​r​a​n​d​o​m​A​d​j​e​c​t​i​v​e​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​a​d​j​e​c​t​i​v​e​I​n​d​e​x​]​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​[​r​a​n​d​o​m​N​o​u​n​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​n​o​u​n​I​n​d​e​x​]​]​;​

 ​ ​ ​ ​N​S​S​t​r​i​n​g​ ​*​r​a​n​d​o​m​N​a​m​e​ ​=​ ​[​N​S​S​t​r​i​n​g​ ​s​t​r​i​n​g​W​i​t​h​F​o​r​m​a​t​:​@​"​%​@​ ​%​@​"​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​r​a​n​d​o​m​A​d​j​e​c​t​i​v​e​L​i​s​t​[​a​d​j​e​c​t​i​v​e​I​n​d​e​x​]​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​r​a​n​d​o​m​N​o​u​n​L​i​s​t​[​n​o​u​n​I​n​d​e​x​]​]​;​

 ​ ​ ​ ​i​n​t​ ​r​a​n​d​o​m​V​a​l​u​e​ ​=​ ​a​r​c​4​r​a​n​d​o​m​(​)​ ​%​ ​1​0​0​;​

 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​r​e​t​u​r​n​ ​n​e​w​I​t​e​m​;​
}​

Build and run to confirm that the program works the same as before.

The nested brackets that you end up with can make things confusing because they are used in two distinct ways: one use sends a message and the other use accesses items in an array. Sometimes, it can be clearer to stick with sending the typed-out message. Other times, it is nice to avoid typing the finger-numbing objectAtIndex:.

Whichever syntax you use, it is important to understand that there is no difference in your application: the compiler turns the shorthand syntax into code that sends the objectAtIndex: message.

In an NSMutableArray, you can use a similar shorthand syntax to add and replace objects.

N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​i​t​e​m​s​ ​=​ ​[​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
i​t​e​m​s​[​0​]​ ​=​ ​@​"​A​"​;​ ​/​/​ ​A​d​d​ ​@​"​A​"​
i​t​e​m​s​[​1​]​ ​=​ ​@​"​B​"​;​ ​/​/​ ​A​d​d​ ​@​"​B​"​
i​t​e​m​s​[​0​]​ ​=​ ​@​"​C​"​;​ ​/​/​ ​R​e​p​l​a​c​e​ ​@​"​A​"​ ​w​i​t​h​ ​@​"​C​"​

These lines are equivalent to sending insertObject:atIndex: and replaceObjectAtIndex:withObject: messages to items.

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

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