开发者

"NSSet allObjects" does random ordering?

开发者 https://www.devze.com 2023-04-04 01:56 出处:网络
I have the following code: self.temporaryImageArray = [(NSSet *)[[array objectAtIndex:0] images] allObjects]

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.


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)


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


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

Here's the link

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号