Key value Encoding-KVC

Source: Internet
Author: User

KVC is the abbreviation for key,value,coding, which is the key-value encoding. In iOS, property information for an object can be accessed indirectly through the name of the class's property (key).

Build a project, create a person class, which has two properties, name and age.

Person.h:

#import <Foundation/Foundation.h>

@interface Person:nsobject

{

NSString * name,age;

}

@end

ViewController.h:

The person class header file is introduced in the ViewController.h file, and Viewcontroller has a property of the person type Jay.

#import <UIKit/UIKit.h>

#import "Person.h"

@interface Viewcontroller:uiviewcontroller

@property (nonatomic,retain) person * JAY;

@end

VIEWCONTROLLER.M:

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Create a Person object Jay

_jay=[[person Alloc]init];

Setting objects with Key,value information for Jay

[_jay setvalue:@ "Tangled Lun" forkey:@ "name"];

[_jay setvalue:@ "forkey:@" "Age"];

Information about this object is accessed through the properties of the person.

NSLog (@ "%@", [_jay valueforkey:@ "name"]);

NSLog (@ "%@", [_jay valueforkey:@ "Age"]);

}

Printing results:

2015-03-21 15:31:27.536 omg[1677:116165] Tangled Lun

2015-03-21 15:31:27.537 omg[1677:116165] 30

A class (Person) object (Jay) sets the information for an object's properties through the Setvalue:forkey statement, where key is the property of the class (person) and the zodiac of the object (Jay), where the key is name and age, respectively. Set their information through key. The Valueforkey is then called by the object: The statement can access the property information corresponding to the key.

Key value Encoding-KVC

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.