Does fast enumeration in Objective-C guarantee the

2019-01-23 11:10发布

问题:

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]);
}

回答1:

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.



回答2:

Once again I've found the answer right after posting. My old reference didn't mention the order of iteration, but the online one did. The array is indeed iterated in order.