tags:

views:

110

answers:

2

Are there any issues I may be missing using fastEnumeration with the enumerator below? I only ask as code examples (online & books) always seem to use a whileLoop. The code sniped works fine, just curious if I am missing a potential issue. Please not this is just a test with no error checking.

NSDirectoryEnumerator *dirEnum;
NSString *eachPath;

dirEnum = [fileManager enumeratorAtPath:sourceDir];
for(eachPath in dirEnum) NSLog(@"FILE: %@", eachPath);

gary

A: 

Although its often not mentioned you can use fast enumeration on any NSEnumerator object or subclass (NSDirectoryEnumerator) object.

fuzzygoat
+1  A: 

The examples use a while loop because the fast enumeration syntax is a very recent addition to the language. As long as the object following the in keyword conforms to the NSFastEnumeration protocol, your code is fine.

As a practical matter, since the default implementation in NSEnumerator just falls through to -nextObject, you're not likely to see a noticeable difference in speed either way.

NSResponder
Ah I see, thank you.
fuzzygoat