8

Pretty straightforward question here: is the enumeration order in a for loop guaranteed to follow the order of an NSArray target (i.e. start at object index 0 and increment by 1 each time)?

Specifically, is the enumeration order in the following code snippet guaranteed to start at array index 0 and increment by 1 each loop? (codesArray is an NSArray)

for (NSNumber *num in codesArray) {
    // do stuff //
}

Or if I want to guarantee enumeration order do I have to do a traditional for loop of the style:

for (int i=0; i<[codesArray count]; i++) {
    // do stuff //
}

Thanks!

Murdock
  • 826
  • 9
  • 21

1 Answers1

7

Yes, these will follow a guaranteed order as you would expect.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
  • 2
    Answer here (to same question) has the same conclusion, with a link to Apple's documentation: http://stackoverflow.com/a/2777629/189687 – Max Feb 22 '14 at 07:39