I must be under some strange misconception ...
Consider:
NSSet * mySet = [NSSet setWithArray:@[@1, @9, @3]] ;
NSOrderedSet * mySortedSet = [NSOrderedSet orderedSetWithSet:mySet] ;
for (NSNumber * n in mySet) {
NSLog(@"unsorted: %@", n) ;
}
for (NSNumber * n in mySortedSet) {
NSLog(@"--sorted: %@", n) ;
}
And the console log:
2014-05-05 20:38:46.645 WoGa[50557:60b] unsorted: 9
2014-05-05 20:38:48.675 WoGa[50557:60b] unsorted: 1
2014-05-05 20:38:49.875 WoGa[50557:60b] unsorted: 3
2014-05-05 20:38:54.277 WoGa[50557:60b] --sorted: 9
2014-05-05 20:38:55.544 WoGa[50557:60b] --sorted: 1
2014-05-05 20:38:56.684 WoGa[50557:60b] --sorted: 3
Huh? How is that 'ordered' ? I have no problem sorting myself if needs be, but what exactly is an NSOrderedSet then?
I can accept that the order of the elements in mySet
is unpredictable.
But what is [NSOrderedSet orderedSetWithSet:mySet]
supposed to achieve if not sorting in some order or other ?
From the documentation:
You can use ordered sets as an alternative to arrays when the order of elements is important and performance in testing whether an object is contained in the set is a consideration—testing for membership of an array is slower than testing for membership of a set.
Again, what is the order that NSOrderedSet orderedSetWithSet:
is supposed to provide you with?
You are confusing "ordered" and "sorted". An array is an ordered collection, which means it has a first, second, third, up to a last element. A set or dictionary is an unordered collection, with no ordering of the elements. No first or last element.
The NSOrderedSet combines the properties of NSSet and NSArray. Like an array, you can add elements at the end or insert them anywhere in the array, or remove elements anywhere, or sort the elements. Like an NSSet, you can look up whether an element is in already present in constant time.
Ordered collections can be sorted, but they are not automatically sorted. Unordered collections cannot be sorted, because they are not ordered.
On the other hand, you could have just read the documentation of NSOrderedSet which makes this all quite clear.