I know that NSSet does not preserve order and I got curious about how the iteration order is implemented in iOS4/iOS5 sdks. Do you have any ideas?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short Answer:
The order is random.
Long Answer:
By definition, the order is not specified. In other words, it depends on the implementation.
In common implementations, every object is converted to an integer number (hash code) which is used to find the object in the set quickly (indexing hash table) and the iteration order is then implemented by hash codes (a hash code is basically an array index).
Note that if two objects have the same hash code (but they are not equal), their order depends only on the sequence of insert/delete operations on the set.
Since common hash table implementations use different hash functions for different data sizes, the order can change completely when set size is increased.