Re: NSArray enumerator orders- interface or implementation detail? also: removing conditional statements
Re: NSArray enumerator orders- interface or implementation detail? also: removing conditional statements
- Subject: Re: NSArray enumerator orders- interface or implementation detail? also: removing conditional statements
- From: "John C. Randolph" <email@hidden>
- Date: Mon, 7 Oct 2002 14:48:34 -0700
On Monday, October 7, 2002, at 12:21 PM, Joe Osborn wrote:
In short-- can I rely upon -[NSArray objectEnumerator] to return an
enumerator which will enumerate through the array's elements in the
order of their indices in the array?
Yes. You can also rely on [NSArray reverseObjectEnumerator] to iterate
through the array in reverse order.
Can I sort the array by some arbitrary criterion and then get an
enumerator that is likewise sorted?
You can make a sorted array using -sortedArrayUsingFunction:context: or
-sortedArrayUsingSelector:, and if you ask the sorted array for an
enumerator, it will return the objects in the sorted order.
I know that the answers to these questions are yes- but... is the
fact that their answer is such an integral part of the interface? Or
is it merely an implementation detail that could differ between
implementations/versions of NSArray?
NSArray is an ordered collection. If Apple broke NSArray like you
suggest, a lot of our code would quit working.
I believe that other collections return enumerators with objects in
undefined order.
NSSet and NSDictionary will both give you enumerators which will return
the objects in the collection in an order dependent on the hash
function.
It's obvious I can't rely on those to be in any particular order,
because they're not ordered collections.
So: For ordered collections, is it customary(required?) to always
return via objectEnumerator an enumerator whose objects are ordered as
the ordered collection's were?
Yes.
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.