Re: NSArray for (... in ...)
Re: NSArray for (... in ...)
- Subject: Re: NSArray for (... in ...)
- From: "Adam R. Maxwell" <email@hidden>
- Date: Fri, 23 May 2008 21:17:59 -0700
On May 23, 2008, at 8:55 PM, Quincey Morris wrote:
Incidentally, since the 'for (... in ...)' syntax got mentioned
earlier today, I'm prompted to ask whether anyone has found a
guarantee in the documentation (or in the header files, for that
matter) that, when the object being fast-enumerated is a NSArray,
the contents are returned in array index order. I haven't found any
written promises to this effect so far.
It may *seem* obvious that this should be the order of enumeration,
but I hesitate to rely on it if it's not documented.
It is documented to return elements in index order. From
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_7_section_1.html#/
/apple_ref/doc/uid/TP30001163-CH18
"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."
And, given that it's nearly as easy to "enumerate" a NSArray in
order by iterating through its indexes, I did wonder whether the
"fast" version might be intended to be the fastest possible
enumeration, which might not be the index order if the internals of
the object were somewhat complex.
_CFArrayFastEnumeration() in CFArray.c gives some good insight into
the implementation.
--
Adam
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden