|
There are two ways to take value from Nsdictionary, Objectforkey: and Valueforkey: What is the difference between these two methods ?
Let's look at the definition of these two methods from Nsdictionary files:
Objectforkey:returns the value associated with Akey, or nil if no value are associated with Akey. Returns the value of the specified key if this key does not return nil.
Valueforkey:returns the value associated with a given key. Also returns the value of the specified key.
The two approaches seem to have nothing to do with it, but the files are valueforkey: There is an extra point:
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. Via Discussion
In general, key can be any string combination, if the key is not opened at the @ symbol, this time Valueforkey: equivalent to Objectforkey:, if you start with @, remove the @ in the key and then use the rest as key to execute [super Val Ueforkey:].
Like what:
Nsdictionary *dict = [nsdictionary dictionarywithobject:@ "Thevalue"
forkey:@ "Thekey"];
NSString *value1 = [dict objectforkey:@ "Thekey"];
NSString *value2 = [dict valueforkey:@ "Thekey"];
At this time value1 and value2 are the same results. If this is a dict:
Nsdictionary *dict = [nsdictionary dictionarywithobject:@ "Thevalue"
forkey:@ "@theKey"];//note that this key is opened at @
NSString *value1 = [dict objectforkey:@ "@theKey"];
NSString *value2 = [dict valueforkey:@ "@theKey"];
Value1 can correct the value, but the value2 value will be directly crash off, the wrong information:
Terminating app due to uncaught exception ' nsunknownkeyexception ', Reason: ' [<__nscfdictionary 0x892fd80> Valueforundefinedkey:]: This class was not key value coding-compliant for the key Thekey. '
This is because Valueforkey: is KVC (nskeyvaluecoding) method, in KVC can pass the property of the same name string to obtain the value of the response. Like what:
@interface Person:nsobject
@property (nonatomic, retain) NSString *name;
@end
...
Person *person = [[Person alloc] init];
Person.name = @ "Fannheyward";
NSLog (@ "name:%@", [person name]);
Name:fannheyward
NSLog (@ "name:%@", [Person valueforkey:@ "name"]);
Name:fannheyward
[Person release];
Valueforkey: The value is to find the property accessor with the same name as the specified key, Valueforundefinedkey is not executed at the time:, and Valueforundefinedkey: The tacit reality is to throw out Nsundefinedkeyexception is very common.
Back to see just crash example, [Dict valueforkey:@ "@theKey"]; Will remove the @ in the key, it becomes the [dict valueforkey:@ "Thekey"], and dict does not exist thekey this kind of property, turn and execute [dict valueforundefinedkey:@] Thekey "], throw out nsundefinedkeyexception and crash out.
Objectforkey: And Valueforkey: In many cases the results are the same, but if the key is at @, Valueforkey: It's a big pit, and it's proposed to use only Objectforkey in the nsdictionary: to take the value.
|