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.

80 CHAPTER 4 Storing data in collections<br />

Notice that at no point do you specify in which order you want to step through the array.<br />

This logic is inherently built into the particular NSEnumerator instance you obtain. Different<br />

versions of NSEnumerator may step through the sequence in different orders.<br />

As an example, try replacing the existing call to objectEnumerator with a call to<br />

reverseObjectEnumerator. Both NSArray messages provide NSEnumerator instances,<br />

but the enumerator provided by reverseObjectEnumerator steps through the array<br />

in reverse order, starting at the last element and working toward the first.<br />

Using an enumerator can be efficient because the NSEnumerator instance can keep<br />

track of additional state about the internal structure of the object it’s enumerating, but<br />

it’s not the only weapon available in <strong>Objective</strong>-C’s toolbox of performance tricks.<br />

FAST ENUMERATION<br />

As the name suggests, fast enumeration is a feature designed to make the process of<br />

enumeration even faster and more efficient. It’s one part <strong>Objective</strong>-C language syntax<br />

and one part runtime library support. For fast enumeration, you can use a special<br />

form of the for statement:<br />

NSEnumerator enumerator = [pets reverseObjectEnumerator];<br />

for (NSString *pet in enumerator) {<br />

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

}<br />

The syntax is cleaner and more concise, but how does it work? In the brackets of the<br />

for statement, you declare a new variable followed by the keyword in and the object<br />

to which you want to apply fast enumeration. Each time through the loop, the for<br />

statement assigns the next element in the enumeration to the specified variable.<br />

Fast enumeration can be used with any NSEnumerator, and it can also be used<br />

directly on an instance of the NSArray or NSMutableArray class. For example, the following<br />

snippet also works and leads to some very clean code:<br />

for (NSString *pet in pets) {<br />

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

}<br />

Fast enumeration can’t be used with every object, however. It requires the object that<br />

is after the in keyword to implement the NSFastEnumeration protocol. Protocols are<br />

discussed in detail in chapter 7.<br />

4.1.5 Adding items to an array<br />

If you create an instance of the NSArray class, the array is said to be immutable because<br />

you can’t modify the structure or contents of the array after it’s been initially constructed.<br />

If you attempt to modify an NSArray instance, you’ll get an exception message<br />

such as the following listed to the Xcode debugger console, and your application<br />

will crash:<br />

*** Terminating app due to uncaught exception<br />

'NSInternalInconsistencyException', reason: '*** - [NSCFArray<br />

replaceObjectAtIndex:withObject:]: mutating method sent to immutable<br />

object'

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

Saved successfully!

Ooh no, something went wrong!