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

前端 未结 5 1102
南笙
南笙 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. ;-)

    0 讨论(0)
  • 2021-02-12 16:36

    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.

    0 讨论(0)
  • 2021-02-12 16:40

    Usually you would want to do something with the keys, in which case it is simpler to use this NSDictionary method:

    - (NSSet<KeyType> *)keysOfEntriesPassingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
    

    This saves time because now you don't need to filter your array using a predicate, you can just perform your test in here and you get a set back. Simple.

    Furthermore, you can take advantage of multi-processor concurrency by passing the concurrent enumeration option to this version of the method:

    - (NSSet<KeyType> *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
    
    0 讨论(0)
  • 2021-02-12 16:51

    My guess is that, since -allKeys returns a copy of the keys (it's not backed by the dictionary) that creating an NSSet is a lot of overhead (building a tree or hash table or whatever) when compared to just dumping the keys into a flat array.

    0 讨论(0)
  • 2021-02-12 16:52

    use use c++, std::map provides access to its keys as a set. the set returned is even "live" , the set reflects the ongoing current set of keys. of course you are free to make a copy as well.

    0 讨论(0)
提交回复
热议问题