Fast Enumeration with NSEnumerator

2 04 2008

While reading The Objective-C 2.0 Programming Language, I found that Fast Enumeration can be used with NSEnumerator(The Objective-C 2.0 Programming Language: Fast Enumeration).

Suppose you have an array which you want to itarete over.

NSArray *tmpArray =
    [NSArray arrayWithObjects:@"first", @"second", @"third", nil];

Use the for statement against the reverseEnumerator obtained from the array. It loops through the array starting from the last element and proceed to the first.

NSEnumerator *enumerator = [tmpArray reverseObjectEnumerator];
for(NSString *s in enumerator) {
    NSLog(@"%@", s);
}