Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn’t [NSDictionary allKeys] return a set?

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.

like image 705
zoul Avatar asked Jan 14 '11 17:01

zoul


2 Answers

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.

like image 172
Chuck Avatar answered Oct 19 '22 16:10

Chuck


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. ;-)

like image 23
Eiko Avatar answered Oct 19 '22 18:10

Eiko