Reputation: 29
As we know,NSDictionary is non-ordered.however,when keys is number,when the dictionary is printed,they are printed by ascending number. i have try many times.
Upvotes: 1
Views: 71
Reputation: 52237
the order of a dictionary depends on the value that is returned by the hash method of the added key-object. it is very like, that NSNumber returns its value, it is was created with an Integer. in that case your observation would be correct.
more infos: http://bynomial.com/blog/?p=73
try ing this code
NSArray *numbers = @[@0,@1,@2,@3];
for (NSNumber *n in numbers) {
NSLog(@"%ld:%@", (unsigned long)[n hash], n);
}
it produces this output
0:0
2654435761:1
5308871522:2
7963307283:3
seems as if Knuth's hash algorithm was implemented half.
The original is
hash(i)=i*2654435761 mod 2^32
here it seems to be just
hash(i)=i*2654435761
Upvotes: 1