Is there a reason for NSDictionary
to return its keys as NSArray
instead of NSSet
? The documentation already states that the order of the keys in the array is undefined, it would sound logical to use a set.
Sets tend to be somewhat neglected in API design. They'll be included most of the time, but usually long after all the other standard data structures. Add on top of that the fact that besides the very recent NSFastEnumeration, there is no general collection or sequence protocol in Objective-C — every collection class is completely independent of all the others — and it becomes very hard to switch to sets after an API has already been written that returns arrays.
My guess is that Apple is using NSArray
all over the place (and most programmers as well), so this choice comes naturally - and changing it now would come at a high cost. And if using arrays internally, a copy of an immutable array is much cheaper than building a set just for the sake of mathematical elegance.
Also note that NSSet
and NSArray
don't have a common parent (well, except NSObject
, of course), so abstracting this interface is also impossible (except for returning something that conforms to NSFastEnumeration
).
Just wild speculation, of course. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With