The difference between iOS development--objectforkey and Valueforkey in Nsdictionary

Source: Internet
Author: User

There are two ways to take a value from Nsdictionary, Objectforkey: and Valueforkey: What is the difference between these two methods?

Let's look at the definitions of these two methods from the Nsdictionary documentation:

Objectforkey:returns the value associated with Akey, or nil if no value are associated with Akey. Returns the value of the specified key, without which the key returns nil.

Valueforkey:returns the value associated with a given key. Also returns the value of the specified key.

Intuitively, these two methods seem to have no difference, but in the document 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 combination of strings, if the key is not at the beginning of the @ symbol, this time Valueforkey: equivalent to Objectforkey:, if it starts with @, remove the @ in key and 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 starts with @

NSString *value1 = [dict objectforkey:@ "@theKey"];

NSString *value2 = [dict valueforkey:@ "@theKey"];

Value1 can be correctly value, but value2 value will be directly crash out, error message:

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 the KVC can be a property with the same name string to get the corresponding value. 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 and specify a key with the same name of the property accessor, no time to execute Valueforundefinedkey:, and Valueforundefinedkey: The default implementation is to throw Nsundefinedkeyexception exception.

Looking back at the example of crash just now, [dict valueforkey:@ "@theKey"]; Will remove the @ in the key, it will become [dict valueforkey:@ "Thekey"], and dict does not exist thekey such property, instead of [Dict valueforundefinedkey:@] Thekey "];, throw nsundefinedkeyexception exception after crash off.

Objectforkey: And Valueforkey: In most cases the result is the same, but if the key is at the beginning of the @, Valueforkey: It is a big pit, it is recommended to use only Objectforkey in the nsdictionary: to take the value.

The difference between iOS development--objectforkey and Valueforkey in Nsdictionary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.