IOS KVC supplement and instance

Source: Internet
Author: User

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 **

 

Related Article

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.