Padin215
Padin215

Reputation: 7484

NSArray and fast enumeration, always go in index order?

will a fast enumeration of an array always go in index order?

for (NSDictionary *zone in myArray)
{
    //do code
}

will that always enumerate from index location 0, 1, 2, ..., n?

i prefer fast enumeration over a standard for loop since it makes the code easier to read.

Upvotes: 8

Views: 1779

Answers (2)

melsam
melsam

Reputation: 4977

For fast enumeration of an array with indexes, use blocks:

[myArray enumerateObjectsUsingBlock:^(NSObject *object, NSUInteger idx, BOOL *stop) {
   ...
   // idx is myArray's index
}];       

Upvotes: 4

Sean
Sean

Reputation: 5820

Yes it will:

For collections or enumerators that have a well-defined order—such as an NSArray or an NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations gives you the proper index into the collection if you need it.

docs here

Upvotes: 17

Related Questions