This construction is a different type of for loop that processes elements in the Objective-C collection, and not in array C. The first part defines the object that is installed into one element in the collection, each run of the cycle, and the second part is a list to be enumerated . For example, the code:
NSArray *array = [NSArray arrayWithObjects:@"foo", @"bar", nil]; for(NSString *string in array) { NSLog(string); }
will print:
foo
bar
By defining an NSString *string , each loop in the loop gets the next object in the NSArray *array .
Similarly, you can use the enumeration with instances of NSSet (where the order of the objects is not defined) and NSDictionary (where it will enumerate the keys stored in the dictionary), you can list the values โโby listing by keys, then calling valueForKey: in the dictionary using this key) .
This is very similar to the construct in C:
int array[2] = { 0, 1 }; for(int i = 0; i < 2; i++) { printf("%d\n", array[i]); }
which prints:
0
one
This is just a syntactic way to make the code more readable and hide some of the fancy enumerations that go into enumeration objects in NSArray, NSSet, or NSDictionary. For more information, see the Fast Enumeration section of the Objective-C 2.0 programming language document.
Tim
source share