243Stepping over an Array (or Dictionary)
NSArray *paths = [[NSFileManager defaultManager]
directoryContentsAtPath: NSHomeDirectory()];
// either like that...
NSEnumerator *e = [paths objectEnumerator];
for (NSString *p in e) {
NSLog(@"path: %@", p);
}
// or like that.
for (NSString *p in [paths objectEnumerator]) {
NSLog(@"path: %@", p);
}
NSDictionary has both [myDict objectEnumerator]
and [myDict keyEnumerator]
.
Fast Enumberation is a feature in Objective-C 2.0, it allows you to step over arrays and dictionaries in a fast, consice, and secure (guarded against mutations) manner.
for (NSString *p in paths) {
NSLog(@"path: %@", p);
}
// or
NSString *p;
for (p in paths) {
NSLog(@"path: %@", p);
}
Sometimes things are really as simple as they should be.