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