01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Arrays<br />

79<br />

4.1.4 Iterating through arrays<br />

Using the count and objectAtIndex: messages of the NSArray class, you can loop<br />

over each element in an array with a code snippet similar to the following:<br />

int i;<br />

for (i = 0; i < [pets count]; i++) {<br />

NSString *pet = [pets objectAtIndex:i];<br />

}<br />

NSLog(@"Pet: %@", pet);<br />

This code may not be the most efficient or cleanest of techniques. Each iteration<br />

through the loop, when it’s time to evaluate the condition i < [pets count], you are<br />

asking the array to recalculate its length; and depending on how the array is internally<br />

stored (a dynamic array or linked list), repeatedly calling objectAtIndex: may not be<br />

efficient because of repeated walks through the data structure to reach the required<br />

element. One solution <strong>Objective</strong>-C provides for more efficient iteration through a<br />

data structure is the enumerator.<br />

NSENUMERATOR<br />

An enumerator is an object that allows you to step through a sequence of related items in<br />

an efficient manner. In <strong>Objective</strong>-C an enumerator is represented by the NSEnumerator<br />

class. Because an enumerator must step through a set of data, you typically ask a data<br />

structure such as an array to create a suitable enumerator on your behalf rather than<br />

create an NSEnumerator instance directly.<br />

As an example, the following code snippet uses NSArray’s objectEnumerator message<br />

to obtain an NSEnumerator instance that enables you to step through each type of<br />

pet stored in the pets array:<br />

NSArray *pets = [NSArray arrayWithObjects:@"Cat", @"Dog", @"Rat", nil];<br />

NSEnumerator *enumerator = [pets objectEnumerator];<br />

NSString *pet;<br />

while (pet = [enumerator nextObject]) {<br />

NSLog(@"Pet: %@", pet);<br />

}<br />

The objectEnumerator method returns an instance of the NSEnumerator class that’s<br />

suitable for enumeration of each element in the specified array.<br />

NSEnumerator is a simple class that provides a single method called nextObject.<br />

This method, as its name suggests, returns the next object in the sequence the enumerator<br />

is enumerating over. By placing it in a while loop, you’ll eventually be provided<br />

with the value of each element in the array. Once you reach the end of the<br />

sequence, nextObject returns nil to indicate the end of the sequence (another reason<br />

NSArray can’t store the value nil). Executing the code snippet on the array of pet<br />

types results in output similar to the following:<br />

Pet: Cat<br />

Pet: Dog<br />

Pet: Rat

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!