IOS KVC supplement and instance
How does KVC supplement and instance get value and modify value --> How
-(Void) setValue :( id) value forKey :( NSString *) key-(id) valueForKey :( NSString *) key // uses the key as the identifier to obtain the corresponding property value-(void) setValue :( id) value forKey :( NSString *) key // use the key as the identifier to set its corresponding property value-(id) valueForUndefinedKey :( NSString *) key-(void) setNilValueForKey :( NSString *) key '''demo creates a QYPerson class inherited from NSObject QYPerson. h''' # import
@ Interface QYPerson: NSObject @ property (nonatomic, strong) NSString * name; @ property (nonatomic, assign) int age;-(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 attribute in QYPerson.
Monitor the name attribute in QYPerson in the. m file
# Import QYPersonMonitor. h @ implementation QYPersonMonitor // 2. callback method when the observed value changes, this method will be called-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {if ([keyPath isEqual: @ name]) {NSLog (@ change name: old: % @, new: % @, [change objectForKey: NSKeyValueChangeOldKey], [change objectForKey: NSKeyValueChangeNewKey]); }}@ end
Initialize the Monitored object and Monitored object in the controller, and register the observer.
# Import ViewController. h # import QYPerson. h # import QYPersonMonitor. h @ interface ViewController () @ property (nonatomic, strong) QYPerson * person; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // initialize the Monitored object _ person = [[QYPerson alloc] init]; _ person. name = @ zhangsan; _ person. age = 18; // monitoring object QYPersonMonitor * personMonitor = [[QYPersonMonitor alloc] init]; // 1. registered an observer [_ person addObserver: personMonitor forKeyPath: @ name options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: nil]; // The Initial Value NSLog (@ person's name is % @, _ person. name); // using the setvalue method, QYPersonMonitor monitoring will be called [_ person setValue: @ (lisi by KVC) forKey: @ name]; // print the NSLog (@ person's name get by kvc is % @, [_ person valueForKey: @ name]) after modification through kvc; // pass. the effect of syntax modification is the same as that of setvalue. name = @ name change. name = wangwu; // use the person function to change the name attribute [_ person changeName];} @ end
The output is as follows:
** 16:38:59. 802 SetDemo [3764: 1391043] person's name is zhangsan
16:38:59. 802 SetDemo [3764: 1391043] change name: old: zhangsan, new: (lisi by KVC)
16:38:59. 802 SetDemo [3764: 1391043] person's name get by kvc is (lisi by KVC)
16:38:59. 802 SetDemo [3764: 1391043] change name: old :( lisi by KVC), new: name change by. name = wangwu **