KVC/KVO, kvckvo

Source: Internet
Author: User

KVC/KVO, kvckvo

1 // KVC (Key Value Coding) 2 // assign values to attributes of an object, 3 Student * stu = [[Student alloc] init]; 4 [stu setValue: @ "xiaowang" forKey: @ "name"]; 5 NSLog (@ "% @", stu. name); 6 7 8 // KVO (Key Value Observer) 9 // Add the observer 10 to the student object // The first parameter is who observes the student 11 // The second parameter is the observed student's attribute 12 // The third parameter is the observation mode 13 14 // instantiate the Student object 15 stu = [[Student alloc] init]; 16 [stu setValue: @ "zhangsan" forKey: @ "name"]; 17 [stu addObserver: self forKeyPath: @ "name" options: available | NSKeyValueObservingOptionOld context: nil]; 18 // set the name attribute of the student object to xiaowang19 [stu setValue: @ "xiaowang" forKey: @ "name"];
// The Observer calls this method when it finds that the observed attribute is changed. The observer writes this method to the observer. m-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {NSLog (@ "new: % @", [change objectForKey: @ "new"]); NSLog (@ "old: % @", [change objectForKey: @ "old"]);}

 

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.