NSDictionary allKeys returns an NSArray instead of an NSSet

I wonder which solution for the NSDictionary allKeys method returns an NSArray rather than an NSSet, which will make more sense, since there is no guarantee on the order of the returned array and, in addition, the key is unique

+6
source share
2 answers

Well, how everyone guesses ...

As the OP said, each key is unique. For an array or data set, the dictionary code must collect the keys - the cost of this applies to both. In the given case, each key should be added to the set, which includes (membershipless, as it turns out) a membership test, while in the case of an array, a membership test is not required - the cost of adding to the array should be less than adding to the set. The array wins.

+1
source

And besides what Justin said, if you really need a list of dictionary keys, you will need to get an NSSet and then convert it to an NSArray. If you just want to check for the existence of a key, OTOH, you can go directly to the dictionary and not need a set.

+1
source

Source: https://habr.com/ru/post/950657/


All Articles