1

Possible Duplicate:
Does fast enumeration in Objective-C guarantee the order of iteration?

Just a quick question that was proving problematic to google:

When fast-iterating over an NSArray like so:

for (NSObject *obj in array) {}

Is the order well-defined? In my tests it seems to be, but I wasn't able to find a guarantee anywhere...

Thanks!

Community
  • 1
  • 1
elsurudo
  • 3,579
  • 2
  • 31
  • 48

2 Answers2

4

From the Fast Enumeration section of The Objective-C Programming Language:

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.

yuji
  • 16,695
  • 4
  • 63
  • 64
0

Yes, it does.

The documentation says NSArray and its subclasses «manage ordered collections of objects». This is the guarantee you are looking for.

Note this is not the case with NSDictionary, for instance.

Macmade
  • 52,708
  • 13
  • 106
  • 123