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

前端 未结 5 1104
南笙
南笙 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:40

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

    - (NSSet *)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 *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
    

提交回复
热议问题