The counterpart to setObject:forKey:
is
objectForKey:
(and not valueForKey:
) to retrieve an item from a dictionary:
NSString *theObject = [dict objectForKey:@"@"];
Alternatively, use the "new" dictionary subscripting syntax:
dict[@"@"] = @"Some Object";
NSString *theObject = dict[@"@"];
valueForKey:
uses Key-Value coding methods if the key starts with @
.
From the documentation of -[NSDictionary valueForKey:]
:
If key does not start with “@”, invokes
objectForKey:
. If key does start with “@”, strips the “@” and invokes[super valueForKey:]
with the rest of the key.
For example,
NSString *x = [dict valueForKey:@"@description"];
does the same as
NSString *x = [dict description];
So in almost all cases, you should use objectForKey:
, unless you explicitly
want to do some Key-Value coding magic.