Fast enumeration is an Objective-C’s feature that helps in enumerating through a collection.
Collections in Objective-C
Collections are fundamental constructs. It is used to hold and manage other objects. The whole purpose of a collection is that it provides a common way to store and retrieve objects efficiently.
There are several different types of collections. While they all fulfil the same purpose of being able to hold other objects, they differ mostly in the way objects are retrieved. The most common collections used in Objective-C are:
NSSet
NSArray
NSDictionary
NSMutableSet
NSMutableArray
NSMutableDictionary
Fast enumeration Syntax
for (classType variable in collectionObject )
{
statements
}
Here is an example for fast enumeration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#import <Foundation/Foundation.h> int main() { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSArray *array = [[NSArray alloc] initWithObjects:@"string1", @"string2",@"string3",nil]; for(NSString *aString in array) { NSLog(@"Value: %@",aString); } [pool drain]; return 0; } |