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

前端 未结 5 1105
南笙
南笙 2021-02-12 16:10

Is there a reason for NSDictionary to return its keys as NSArray instead of NSSet? The documentation already states that the order of the

5条回答
  •  执笔经年
    2021-02-12 16:31

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

提交回复
热议问题