About KVC Key-value encoding

Source: Internet
Author: User

Reprinted from Http://www.tuicool.com/articles/2aYfy2

Key-value coding, which is a mechanism that uses string identifiers to indirectly access object properties, rather than calling getter and setter methods directly. Often we use Valueforkey instead of getter methods, Setvalue:forkey to replace setter methods.

First look at the difference between KVC and Setter,getter method assignment

Persion *person =  [[Person alloc] init]; // do not use KVC @" Hufeng "  ; // using the KVC notation [Person  SetValue:@ "hufeng" forkey:@ "name" ];

Here we write a complicated point: we have a person this person has a cell phone this phone class has a battery class we want to get this battery class.

// Using Attributes Persion *person =  *phone =*battery = phone.battery;
Using KVC
@" Phone.battery " ];

Note-The value inside the Valueforkeypath is case-sensitive, and you can't do it if you write the phone.battery.

The most common use of KVC is in serializing and deserializing session objects. We often need to deserialize the JSON string into the object we want. Below is an example of serializing a dictionary into an object using Nskeyedarchiver.

-(ID) initwithdictionary: (nsdictionary *) dictionary {    = [self init];     if (self) {        [self setvaluesforkeyswithdictionary:dictionary];    }     return Self ;}

About KVC Key-value encoding

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.