I was wondering how keys work in a NSDictionary. Usually, I will use a NSString as a key for example:
NSString *stringKey = @"stringKey";
[mydict objectForKey:stringKey];
What if I wanted to use a NSNumber:
NSNumber *numberKey = [NSNumber numberWithInt:3];
[mydict objectForKey:numberKey];
Does the dictionary go look for the key with number 3? or would it just compare the address of the numberKey?
An object representing a static collection of key-value pairs, for use instead of a Dictionary constant in cases that require reference semantics.
An NSDictionary will retain it's objects, and copy it's keys.
Two keys are equal if and only if [key1 isEqual:key2]
. Some classes may go with the -[NSObject isEqual:]
implementation of return self == other;
, but it's quite common for classes (such as NSString
, NSNumber
, etc) to override it to do more context-specific comparison.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With