Octoshape
Octoshape

Reputation: 1141

"NSSet allObjects" does random ordering?

I have the following code:

self.temporaryImageArray = [(NSSet *)[[array objectAtIndex:0] images] allObjects]

Array holds an Band object from my CoreData model. It has an NSSet as a property called "images".

Now I use this temporaryImageArray to determine via timestamps whether or not the images need to be updated. I have come across some very random behavior and my question now is:

Does [NSSet allObjects] return the Objects from the Set randomly in no order?

Is there any way to prevent this or to have it return it in order? It would lessen the complexity of my code a lot.

Upvotes: 8

Views: 4557

Answers (3)

jamesC
jamesC

Reputation: 422

NSOrderedSet and NSMutableOrderedSet are now available in iOS 5 just so you're kept up to date.

Here's the link

Upvotes: 3

Lily Ballard
Lily Ballard

Reputation: 185841

NSSet is an unordered collection. It has no idea what the "order" of its objects are. Therefore, when you call -allObjects, it returns them unordered.

Note that the documentation on -allObjects states:

An array containing the set’s members, or an empty array if the set has no members. The order of the objects in the array isn’t defined.

(emphasis mine)

Upvotes: 13

Reed Olsen
Reed Olsen

Reputation: 9169

A set does not have order. However, in 10.7 (Lion), there is an NSOrderedSet class. This isn't available in iOS 4.0.

Upvotes: 4

Related Questions