Objective-c KVC Key-value pairs

Source: Internet
Author: User

KVC key value pair is later in the development of the project will often use, if it is not clear that is key, that is value, then it will be very disadvantage, I personally is in this area to eat a lot of loss, especially in the Address book. Maybe the first touch is just a simple dictionary, or a simple array, but wait until later encountered in the dictionary contains an array, array of arrays, and then in the set of dictionaries, and so on, then SB.

KVC: All spelling is (key-value-coding), key value encoding. is an indirect way to access an instance variable. Key: Keys that identify instance variables. Value: is the value that corresponds to the instance variable. Here are some of the ways KVC uses:

First: How to modify a value

1.setvalue:forkey:

2.setvalue:forkeypath:

3.setvalue:forundefinedkey:

4.setValuesForKeysWithDicitonary:

Second: Get the value

1.valueForKey:

2.valueForKeyPath:

3.valueForUndefinedKey:

Today we only say two ways that we often use:

1.setvalue:forkey:

The first thing to do is to define two properties in the Student.h class.

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *sex;

Use KVC in the main function

Student *stu = [[Student alloc]init]; Create a Student object

[Stu setvalue:@ "Dandan" forkey:@ "name"]; use KVC to assign values

Here is the value you want to set, this value is the value of name, I changed to "Dandan", the value has, then to find a corresponding property variable, Forkey is the name you defined in Student.h. This key should be the same as your property definition.

2.setvalue:forundefinedkey:

[Stu setvalue:@ "" forundefinedkey:@ "weight"];

First of all, we do not define the properties of weight in Student.h, so it is not found, according to the common sense that you can not find the key, the compiler should be error, but use this method will not error, but also will prompt us: there is no corresponding instance variable, This method is often used when doing projects at a later stage. REMEMBER!!!!!

3.setValuesForKeysWithDictionary:

Now there is a need to assign a value to an object through a dictionary, then we have to use the method above. First we will create a Dictionary object.

Nsdictionary *datadic = [[Nsdictionary alloc]initwithobjectsandkeys:@ "Jack", @ "name", @ "male", @ "sex", nil];

[Stu Setvaluesforkeyswithdictionary:datadic];

So we can assign a value to the Stu object through a dictionary. Maybe you can't understand it all at once, so you can practice more in the project.

Objective-c KVC Key-value pairs

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.