Reputation: 9140
I have the following NSDictionary:
NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
But when I try to query the dictionary using valueForKey I always get nil value. For example:
po [myDict valueForKey:@"value1"]
nil
If query the dictionary using objectForKey everything is Ok..
po [myDict objectForKey:@"key1"]
value1
Any of you knows why I'm getting nil when I query the dictionary using valueForKey ?
I'll really appreciate your help.
Upvotes: 0
Views: 118
Reputation: 318944
valueForKey:
should only be used when you have a clear and understood need to use key-value coding. You still need to pass in a key but you are passing in a value. Since there is no key in the dictionary of value1
, it correctly returns nil
.
But again, don't use valueForKey:
unless you have a proper reason.
objectForKey:
on key1
works because you actually have a key of key1
in the dictionary.
If you were to call objectForKey:@"value1"
it would also return nil
.
Upvotes: 1