开发者

Fast Enumeration With enumeratorAtPath?

开发者 https://www.devze.com 2022-12-13 10:48 出处:网络
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

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: %@", e开发者_StackOverflow中文版achPath);

gary


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消