Does "NSSet allObjects" do random order?

I have the following code:

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

The array contains the Band object from my CoreData model. It has an NSSet as a property called "images."

Now I use this temporaryImageArray to determine through timestamps whether images should be updated. I came across some very random behavior, and now my question is:

Does an object [NSSet allObjects] objects from a set in random order?

Is there a way to prevent this or put it back in order? This will greatly reduce the complexity of my code.

+6
source share
3 answers

The set has no order. However, in 10.7 (Lion) there is an NSOrderedSet class. This is not available in iOS 4.0.

+3
source

NSSet is an unordered collection. He does not know what the "order" of his objects is. Therefore, when you call -allObjects , it returns them unordered.

Note that the documentation on -allObjects states:

An array containing elements of the set or an empty array if there are no elements in the set. The order of the objects in the array is not defined.

(my emphasis)

+10
source

NSOrderedSet and NSMutableOrderedSet are now available in iOS 5 to keep you updated.

Here is the link

+3
source

Source: https://habr.com/ru/post/897024/


All Articles