是否在快速枚举Objective-C的保证迭代的顺序? [英] Does fast enumeration in Objective-C guarantee the order of iteration?
问题描述
我可以期望它从一个数组开始到年底才能?找不到这个的文档任何东西。
即。是
为(ID VAL数组)
{
的NSLog(@%@,VAL);
}
总是要打印出来的一样。
的for(int i = 0; I<阵列计数]; ++ I)
{
的NSLog(@%@,[阵列objectAtIndex:i]);
}
从苹果'<一个href=\"http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/FoundationTypesandCollections/FoundationTypesandCollections.html\">Objective-C在快速枚举文档:
有关集合或枚举具有明确的订单,如从上按顺序一个数组枚举收益,所以如果你需要它只是计数的迭代会给你适当的索引集派生的NSArray或NSEnumerator实例
块引用>Can I expect it to go from the start of an array to the end in order? Can't find anything in the docs about this.
i.e. is
for (id val in array) { NSLog(@"%@", val); }
always going to print out the same as
for (int i = 0; i < [array count]; ++i) { NSLog(@"%@", [array objectAtIndex:i]); }
解决方案From Apples' Objective-C documentation on fast enumeration:
For collections or enumerators that have a well-defined order—such as NSArray or NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations will give you the proper index into the collection if you need it.
这篇关于是否在快速枚举Objective-C的保证迭代的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!