开发者

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

Is there a reason for NSDictionary to return its keys a开发者_JAVA技巧s 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. ;-)


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜