01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

78 CHAPTER 4 Storing data in collections<br />

NSLog(@"We found '%@' within the array",<br />

valueWeAreLookingFor);<br />

} else {<br />

NSLog(@"We didn't find '%@' within the array",<br />

valueWeAreLookingFor);<br />

}<br />

In this listing you set up a loop B to step through each element in the array. The loop<br />

sets the loop counter variable i to the values 0 through [pets count] - 1. In the loop<br />

you fetch the array element at index i via the objectAtIndex: method and check to<br />

see if it’s equal to the value you are looking for c. If a match is found, you set the<br />

found variable to YES d and break out of the for loop immediately, because there’s<br />

no need to check any remaining array elements.<br />

We think that’s a lot of code for such a simple task! It also allows ample opportunity<br />

for subtle bugs to creep in. Luckily, Foundation Kit provides a much more convenient<br />

technique to check if an array contains a specific value in the form of NSArray’s<br />

containsObject: message:<br />

BOOL found = [pets containsObject:@"Fish"];<br />

if (found) {<br />

NSLog(@"We found 'Fish' within the array");<br />

}<br />

Internally, containsObject: performs a similar search through the array, comparing<br />

each element as it goes; however, this action is hidden from you, and more important,<br />

there’s no opportunity for you to introduce errors. This technique demonstrates<br />

another advantage of using objects: the ability to easily reuse methods instead of continually<br />

writing them from scratch.<br />

Sometimes you’re interested in determining not only if a value exists in an array<br />

but also its position. The indexOfObject: message performs a similar task to containsObject:,<br />

but instead of returning a Boolean flag to indicate if the object is<br />

found, it returns the index at which the item is found or a special value NSNotFound if<br />

it’s not found:<br />

int indexOfItem = [pets indexOfObject:@"Dog"];<br />

if (indexOfItem != NSNotFound) {<br />

NSLog(@"We found the value 'Dog' at index %d", indexOfItem);<br />

}<br />

Although messages such as indexOfObject: and containsObject: allow you to<br />

remove, or at least hide, logic that loops over each element in an array, some cases<br />

may require such logic. For example, you may want to convert the name of each pet<br />

into uppercase form, and NSArray has no built-in method to achieve this task. But<br />

<strong>Objective</strong>-C and Foundation Kit provide mechanisms to perform iteration over an<br />

array more efficiently and safely than was done in listing 4.2.

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

Saved successfully!

Ooh no, something went wrong!