[IOS] KVC and KVO

Source: Internet
Author: User
Tags notification center

I. KVC and KVO
* "KVC": key value Coding * objective: to indirectly modify or obtain object attributes and reduce coupling between programs (classes and classes.
* "KVO": key value Observer (key value observation), Observer mode. (multiple methods are used to detect model changes) * objective: to make a response in a timely manner when an attribute of an object changes.
Ii. Usage of KVC

KVC is called the iOS development platform !!! You can quickly modify object attributes.

* [P1setValue: @ "xxxx" forKeyPath: @ "name"]; modifies the attributes of a specified object. * [arrvalueForKeyPath: @ "book. bookName "]; get object attributes. * KVC can directly obtain the array through the key. * When Using KVC, make sure that the key value exists. * principle: If the KVC value does not contain the specified key value, it automatically enters the internal Member of the object for search.
Iii. Usage of KVC
* KVO is usually used to observe the change of "an attribute of an object" and respond accordingly. (mostly used to observe model data) * one difference between KVO and the notification center: KVO can only stare at an object (working in the current region), and the notification center can traverse through many layers (such as viewController ). * KVO usage steps: * 1. [stuaddObserver: selfforKeyPath: @ "name" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: @ "myObserver"]; * 2. observeValueForKeyPath. this method is automatically called when the specified key value changes * 3. removeObserver. all observer modes have poor performance and need to be removed in time.
Directly run the Code:
//// XNViewController. m // KVC----KVO // Created by neng on 14-6-21. // Copyright (c) 2014 neng. all rights reserved. // # import "XNViewController. h "# import" XNPerson. h "# import" XNStudent. h "# import" XNBook. h "@ interface XNViewController () @ end/*** KVC: Key Value Coding (Key Value encoding) * indirectly Modifies/obtains object attributes to reduce coupling between classes. * KVO: Key Value Observer (Key Value observation) KVO is usually used to observe "an object's attribute" and respond in a timely manner! The NSNotificationCenter is an object that requires the POST "Notification string" (indicating the event type of the listener) to exist, so that the notification center can work! * // @ Implementation XNViewController-(void) viewDidLoad {[super viewDidLoad]; // 1. modify the object attribute [self kvcDemo1]; // 2. for subclass, you can also directly modify [self kvcDemo2]; // 3. modify the book attribute of an object. (This is quite troublesome. Create a book object ). KVC statement [self kvcDemo3]; // 4. obtains the property value of an object. it can be obtained in batches (if it is an array ). [self kvcDemo4]; // KVO demo [self kvoDemo];}-(void) kvoDemo {XNStudent * stu = [[XNStudent alloc] init]; stu. name = @ "xuneng "; // set the listening object // 1 the observed object self // 2 the observed key value path // 3 the observed option // 4 the context: it is usually used to differentiate different observation events [stu addObserver: self forKeyPath: @ "name" options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context: @ "myObserver"]; stu. name = @ "neng"; // modify // the performance of all observer modes is poor. You need to promptly remove [stu removeObserver: self forKeyPath: @ "name"]; stu. name = @ "xu"; // modify it again after removal}/** this method name is automatically called when the key value of the object specified by KVO changes: observed key value object: observed object change: Modified Dictionary (new and old values) context: Specifies the context passed in when the Observer */-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context {NSLog (@ "| -- % @ -- |", keyPath, object, change, context );} /*** modify the object attributes */-(void) kvcDemo1 {// 1. original method. modify the object attribute value XNPerson * p1 = [[XNPerson alloc] init]; p1.name = @ "xuneng"; p1.age = 24; // NSLog (@ "% @, % d ", p1.name, p1.age); NSLog (@" KVC1 demo1 --> % @ ", p1); // directly call the description method. // 2. KVC to modify [p1 setValue: @ "xxxx" forKeyPath: @ "name"]; [p1 setValue: @ "10" forKeyPath: @ "age"]; NSLog (@ "KVC2 demo1 --> % @, % d", p1.name, p1.age);}/*** for subclass, you can also directly change */-(void) kvcDemo2 {// 1. traditional Method XNStudent * p1 = [[XNStudent alloc] init]; // student p1.name = @ "student xuneng"; p1.age = 22; // NSLog (@ "% @, % d ", p1.name, p1.age); NSLog (@" KVC1 demo2 --> % @ ", p1); // directly call the description method. // 2. KVC to modify the subclass [p1 setValue: @ "xxxx" forKeyPath: @ "name"]; [p1 setValue: @ "10" forKeyPath: @ "age"]; NSLog (@ "KVC2 demo2 --> % @, % d", p1.name, p1.age);}/*** modify the book attribute of the object. (This is quite troublesome. Create a book object ). KVC statement */-(void) kvcDemo3 {// 1. traditional Method XNStudent * p1 = [[XNStudent alloc] init]; // student p1.name = @ "student xuneng"; p1.age = 22; XNBook * myBook = [[XNBook alloc] init]; myBook. bookName = @ "iOS"; p1.book = myBook; NSLog (@ "KVC1 demo3 --> % @", p1); // 2. KVC method [p1 setValue: @ "iPhone" forKeyPath: @ "book. bookName "]; // book in the student, bookNameNSLog in the book (@" KVC2 demo3 -- >%@ ", p1);}/*** get the object property value. it can be obtained in batches (if it is an array ). */-(void) kvcDemo4 {XNStudent * p1 = [[XNStudent alloc] init]; // student 1p1. name = @ "student1 xuneng"; p1.age = 24; XNBook * myBook1 = [[XNBook alloc] init]; myBook1.bookName = @ "iOS"; p1.book = myBook1; XNStudent * p2 = [[XNStudent alloc] init]; // student 2p2. name = @ "student2 xuneng"; p2.age = 23; XNBook * myBook2 = [[XNBook alloc] init]; myBook2.bookName = @ "iPhone"; p2.book = myBook2; NSArray * arr = @ [p1, p2]; // 1. normal method to obtain the attributes of objects in the array NSMutableArray * arrBook = [[NSMutableArray alloc] init]; for (XNStudent * stu in arr) {[arrBook addObject: stu. book. bookName];} NSLog (@ "KVC1 demo4 --> % @", arrBook); // 2. obtain the NSLog (@ "KVC2 demo4 --> % @", [arr valueForKeyPath: @ "book. bookName "]);} @ end


Download example source code: Http://download.csdn.net/detail/xn4545945/7571719

Reprinted please indicate the source: http://blog.csdn.net/xn4545945





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.