Fast Enumeration
The final major new feature in Objective-C 2.0 is fast enumeration, an Objective-C syntax addition that lets you operate across all the members of a collection simply and easily. Using fast enumeration results in clearer and easier to understand code. For example, if you wanted to iterate over an array of strings, you could use the following code:
NSArray *array =
[NSArray arrayWithObjects:@1, @2, @3, nil];
for (NSString *string in array) {
NSLog(@"string is %@", string);
}
Using fast enumeration is considerably more efficient than using an NSEnumerator object directly. It provides a much more concise syntax. And, best of all, it provides for safe enumeration. If a collection is modified during enumeration, an exception is raised. Since mutation of a collection is forbidden during enumeration, multiple enuermations can be performed on multiple threads concurrently.
Fast enumeration works with any collection that implements the NSFastEnumeration protocol. All of the Cocoa collection classes, including NSArray, NSDictionary, and NSSet, as well as NSEnumerator, implement this protocol. You also add fast enumeration to your own collections by supporting the NSFastEnumeration protocol, which consists of a single method.