Changes

Working with Objective-C Array Objects

626 bytes added, 20:57, 11 November 2009
Accessing the Elements of an Array object
</pre>
== Accessing the Elements of an Array object Object ==
The objects contained in an array are given index positions beginning at position zero. Each element may be accessed by passing the required index position through as an argument to the NSArray ''objectAtIndex'' instance method. We can, therefore, now extend our array example to display each element, using the count method to identify in advance how many elements there are to display:
Element 2 = Blue
Element 3 = Yellow
</pre>
 
== Accessing Array Elements using Fast Enumeration ==
 
The technique for accessing all the array elements using a ''for'' loop as described in the previous section is a little ungainly. Another, easier mechanism for accessing element in an array involves something called ''fast enumeration''. Fast enumeration simply requires that a variable be declared to hold each array element, and then referencing this in the for loop:
 
<pre>
NSArray *myColors;
NSString *color;
 
myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
 
for (color in myColors)
NSLog (@"Element = %@", color);
</pre>