15
NSArray

Like NSString, NSArray is another class that Objective-C programmers use a lot. An instance of NSArray holds a list of pointers to other objects. For example, let’s say you want to make a list of three NSDate objects and then go through the list and print each date.

Create a new project : a Foundation Command Line Tool called DateList. Open main.m and change the main() function:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​t​h​r​e​e​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​t​o​m​o​r​r​o​w​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​y​e​s​t​e​r​d​a​y​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​-​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​a​r​r​a​y​ ​c​o​n​t​a​i​n​i​n​g​ ​a​l​l​ ​t​h​r​e​e​ ​(​n​i​l​ ​t​e​r​m​i​n​a​t​e​s​ ​t​h​e​ ​l​i​s​t​)​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​d​a​t​e​L​i​s​t​ ​=​ ​[​N​S​A​r​r​a​y​ ​a​r​r​a​y​W​i​t​h​O​b​j​e​c​t​s​:​n​o​w​,​ ​t​o​m​o​r​r​o​w​,​ ​y​e​s​t​e​r​d​a​y​,​ ​n​i​l​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​H​o​w​ ​m​a​n​y​ ​d​a​t​e​s​ ​a​r​e​ ​t​h​e​r​e​?​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​r​e​ ​a​r​e​ ​%​l​u​ ​d​a​t​e​s​"​,​ ​[​d​a​t​e​L​i​s​t​ ​c​o​u​n​t​]​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​a​ ​c​o​u​p​l​e​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​ ​f​i​r​s​t​ ​d​a​t​e​ ​i​s​ ​%​@​"​,​ ​[​d​a​t​e​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​0​]​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​T​h​e​ ​t​h​i​r​d​ ​d​a​t​e​ ​i​s​ ​%​@​"​,​ ​[​d​a​t​e​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​2​]​)​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

NSArray has two methods, shown in this example, that you will use all the time:

  • count returns the number of items in an array

  • objectAtIndex: returns the pointer that the array has stored at the index specified by the argument.

Arrays are ordered, and you access an item in the array by its index. Arrays are zero-based: the first item is stored at index 0, the second item is stored at index 1, and so on. Thus, if the count method says there are 100 items in the array, you can use objectAtIndex: to ask for the objects at indices 0 to 99.

Figure 15.1 is an object diagram of your program. Notice that the instance of NSArray has pointers to the NSDate objects.

Figure 15.1  Object diagram for DateList

Object diagram for DateList

Thus, if you want to loop and process each item in the array (or iterate over the array), you can create a for-loop. Edit main.m:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​t​h​r​e​e​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​t​o​m​o​r​r​o​w​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​y​e​s​t​e​r​d​a​y​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​-​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​a​r​r​a​y​ ​c​o​n​t​a​i​n​i​n​g​ ​a​l​l​ ​t​h​r​e​e​ ​(​n​i​l​ ​t​e​r​m​i​n​a​t​e​s​ ​t​h​e​ ​l​i​s​t​)​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​d​a​t​e​L​i​s​t​ ​=​ ​[​N​S​A​r​r​a​y​ ​a​r​r​a​y​W​i​t​h​O​b​j​e​c​t​s​:​n​o​w​,​ ​t​o​m​o​r​r​o​w​,​ ​y​e​s​t​e​r​d​a​y​,​ ​n​i​l​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​U​I​n​t​e​g​e​r​ ​d​a​t​e​C​o​u​n​t​ ​=​ ​[​d​a​t​e​L​i​s​t​ ​c​o​u​n​t​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​i​n​t​ ​i​ ​=​ ​0​;​ ​i​ ​<​ ​d​a​t​e​C​o​u​n​t​;​ ​i​+​+​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​d​ ​=​ ​[​d​a​t​e​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​i​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​H​e​r​e​ ​i​s​ ​a​ ​d​a​t​e​:​ ​%​@​"​,​ ​d​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Programmers do this so often that they made a special addition to the for-loop for iterating over arrays. Edit the code to use this mechanism:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​t​h​r​e​e​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​t​o​m​o​r​r​o​w​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​y​e​s​t​e​r​d​a​y​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​-​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​a​r​r​a​y​ ​c​o​n​t​a​i​n​i​n​g​ ​a​l​l​ ​t​h​r​e​e​ ​(​n​i​l​ ​t​e​r​m​i​n​a​t​e​s​ ​t​h​e​ ​l​i​s​t​)​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​d​a​t​e​L​i​s​t​ ​=​ ​[​N​S​A​r​r​a​y​ ​a​r​r​a​y​W​i​t​h​O​b​j​e​c​t​s​:​n​o​w​,​ ​t​o​m​o​r​r​o​w​,​ ​y​e​s​t​e​r​d​a​y​,​ ​n​i​l​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​N​S​D​a​t​e​ ​*​d​ ​i​n​ ​d​a​t​e​L​i​s​t​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​H​e​r​e​ ​i​s​ ​a​ ​d​a​t​e​:​ ​%​@​"​,​ ​d​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

This type of loop is known as fast enumeration, and it is an extremely efficient way to walk through the items in an array. There is one restriction: you may not add to or remove from the array while enumerating over it.

NSMutableArray

Arrays come in two flavors:

  • An instance of NSArray is created with a list of pointers. You can never add or remove a pointer from that array.

  • An instance of NSMutableArray is similar to an instance of NSArray, but you can add and remove pointers. (NSMutableArray is a subclass of NSArray. More about subclasses in Chapter 18.)

Change your example to use an NSMutableArray instance and methods from the NSMutableArray class:

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​@​a​u​t​o​r​e​l​e​a​s​e​p​o​o​l​ ​{​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​t​h​r​e​e​ ​N​S​D​a​t​e​ ​o​b​j​e​c​t​s​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​n​o​w​ ​=​ ​[​N​S​D​a​t​e​ ​d​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​t​o​m​o​r​r​o​w​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​D​a​t​e​ ​*​y​e​s​t​e​r​d​a​y​ ​=​ ​[​n​o​w​ ​d​a​t​e​B​y​A​d​d​i​n​g​T​i​m​e​I​n​t​e​r​v​a​l​:​-​2​4​.​0​ ​*​ ​6​0​.​0​ ​*​ ​6​0​.​0​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​C​r​e​a​t​e​ ​a​n​ ​e​m​p​t​y​ ​a​r​r​a​y​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​d​a​t​e​L​i​s​t​ ​=​ ​[​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​a​r​r​a​y​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​A​d​d​ ​t​h​e​ ​d​a​t​e​s​ ​t​o​ ​t​h​e​ ​a​r​r​a​y​
 ​ ​ ​ ​ ​ ​ ​ ​[​d​a​t​e​L​i​s​t​ ​a​d​d​O​b​j​e​c​t​:​n​o​w​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​d​a​t​e​L​i​s​t​ ​a​d​d​O​b​j​e​c​t​:​t​o​m​o​r​r​o​w​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​P​u​t​ ​y​e​s​t​e​r​d​a​y​ ​a​t​ ​t​h​e​ ​b​e​g​i​n​n​i​n​g​ ​o​f​ ​t​h​e​ ​l​i​s​t​
 ​ ​ ​ ​ ​ ​ ​ ​[​d​a​t​e​L​i​s​t​ ​i​n​s​e​r​t​O​b​j​e​c​t​:​y​e​s​t​e​r​d​a​y​ ​a​t​I​n​d​e​x​:​0​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​f​o​r​ ​(​N​S​D​a​t​e​ ​*​d​ ​i​n​ ​d​a​t​e​L​i​s​t​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​H​e​r​e​ ​i​s​ ​a​ ​d​a​t​e​:​ ​%​@​"​,​ ​d​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​y​e​s​t​e​r​d​a​y​
 ​ ​ ​ ​ ​ ​ ​ ​[​d​a​t​e​L​i​s​t​ ​r​e​m​o​v​e​O​b​j​e​c​t​A​t​I​n​d​e​x​:​0​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​N​o​w​ ​t​h​e​ ​f​i​r​s​t​ ​d​a​t​e​ ​i​s​ ​%​@​"​,​ ​[​d​a​t​e​L​i​s​t​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​0​]​)​;​

 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​
..................Content has been hidden....................

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