Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary allKeys returns NSArray, instead of NSSet [duplicate]

I wonder what is the decision behind NSDictionary method allKeys, to return an NSArray, instead of an NSSet, which would make more sense, since there is no guarantee about the order of the returned array and moreover, a key is unique

like image 869
Peter Lapisu Avatar asked Jul 30 '13 19:07

Peter Lapisu


1 Answers

Well as everybody is guessing...

As the OP said, every key is unique. For either the array or set case the dictionary code has to collect the keys - the cost of that applies to both. In the set case each key has to be added to the set, which involves a (pointless as it turns out) test of membership, while in the array case no membership test is required - the cost of adding to an array should be smaller than adding to a set. Array wins.

like image 73
CRD Avatar answered Oct 21 '22 20:10

CRD