KVC additions and examples how to get values and modify values-–>how
- (void) SetValue: (ID) Value Forkey: (NSString*) Key-(ID) Valueforkey: (NSString*) Key-(ID) Valueforkey: (NSString*) key//With key as the identifier, get its corresponding property value- (void) SetValue: (ID) Value Forkey: (NSString*) key//Set its corresponding property value with key as the identifier- (ID) Valueforundefinedkey: (NSString*) Key-(void) Setnilvalueforkey: (NSString*) key ' Demo ' creates a Qyperson class that inherits fromNSObjectQyperson. h```#import <Foundation/Foundation.h> @interface qyperson : nsobject @property(nonatomic,Strong)NSString*name;@property(nonatomic,Assign)intage;-(void) ChangeName;@end
Implement the above method in the Qyperson.m file
#import "QYPerson.h"@implementation QYPerson- (void)changeName{ _name = @"changeName";}@end
Create a Qypersonmonitor class to monitor the Name property in Qyperson
Monitoring of the Name property in Qyperson in. m files
#import " QYPersonMonitor.h " @implementation Span class= "Hljs-title" >qypersonmonitor //2. Callback method when the value of the observation changes, the method is called -(void ) Observevalueforkeypath: (nsstring *) KeyPath ofobject: (id ) object change: ( Nsdictionary *) Change context: (void *) context{ if ([KeyPath isequal:@ "name" ]) {nslog (@ "change Name:old:%@, New:%@" , [ch Ange Objectforkey:nskeyvaluechangeoldkey],[change Objectforkey:nskeyvaluechangenewkey]); }} @end
Initializing monitored objects and monitored objects in the controller, registering the viewer
#import "ViewController.h" #import "QYPerson.h" #import "QYPersonMonitor.h" @interface viewcontroller ()@property(nonatomic,Strong) Qyperson *person;@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];//Initialize monitored objects_person = [[Qyperson alloc] init]; _person. Name= @"Zhangsan"; _person. Age= -;//Monitoring ObjectsQypersonmonitor *personmonitor = [[Qypersonmonitor alloc] init];//1. Register an observer[_person addobserver:personmonitor forkeypath:@"Name"options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:Nil];//Initialize the value NSLog(@"Person ' s name is%@", _person. Name);//Through the SetValue method, at which time the Qypersonmonitor monitoring will be called[_person setvalue:@"(Lisi by KVC)"forkey:@"Name"];//Print the value modified by KVC mode NSLog(@"person's name" Get by KVC is%@ ", [_person valueforkey:@"Name"]);//through. The effect of grammar modification is consistent with SetValue_person. Name= @"Name change by. Name= Wangwu";//Change the Name property by person's own function[_person changename];}@end
The printing results are as follows:
**2015-07-29 16:38:59.802 setdemo[3764:1391043] person's name is Zhangsan
2015-07-29 16:38:59.802 setdemo[3764:1391043] Change Name:old:zhangsan, New: (Lisi by KVC)
2015-07-29 16:38:59.802 setdemo[3764:1391043] person's name Get by KVC are (Lisi by KVC)
2015-07-29 16:38:59.802 setdemo[3764:1391043] Change Name:old:(Lisi by KVC), New:name change by. Name= wangwu**
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS KVC Additions and examples