first, KVC and Kvo
*"KVC":key valueCoding( key code ) * purpose : indirect modification or acquisition of object Properties , Reduced program ( classes and Classes ) the degree of coupling between .
* " kvo key value Observer ( key values observe Observer mode . (Test multiple for model changes ) * purpose : Pass is often used to observe The change of an attribute of an object , make the correspondence in time .
Ii. How to use KVC
KVC is called IOS The big trick of developing the platform !!! Can change object properties at high speed.
*[p1setValue: @ "xxxx"forkeypath: @ "name"]; change the specified object properties . *[arrvalueforkeypath: @ "Book.bookname"]; Get Object Properties . *KVC can get the array directly by key . * when using KVC , be sure to ensure that the key value is present . * principle : when the value is KVC , it is assumed that the object that found the value does not include the specified key value , will take the initiative to enter the object internal members to find .
Iii. How to use KVO
*KVO- Pass is often used to observe when an " object property " is changed , make a response. . ( more for observing model data ) * One difference between KVO and notification hubs : KVO can only stare at an object ( working within the current region ), notification hubs can traverse very multilayer (such as Viewcontroller). *KVO Use steps : *1. [Stuaddobserver: selfforkeypath:@ "name" Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:@ "Myobserver"]; *2. Observevalueforkeypath . When the specified key value has changed , will call this method on its own initiative . *3. Removeobserver . all observer patterns are not performing well. , need to be removed in time.
directly on 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 modify/Get the properties of the object, reducing the degree of coupling between class and class. * Kvo:key value Observer (key value observation) KVO is often used to observe that "an attribute of an object" is changed. Respond in a timely manner!
The nsnotificationcenter is the presence of an object that requires the post "notification string", which indicates the type of event being monitored. Notification hubs can work. */@implementation xnviewcontroller-(void) viewdidload {[Super VIEWDIDLOAD];//1. Simple Change object properties [self KVCDEMO1];//2. For subclasses, can also directly change the [self kvcdemo2];//3. Change the book property of an object. (quite troublesome, good to create book object). KVC a word [self kvcdemo3];//4. Gets the property value of the object. Can be obtained in bulk (assuming an array). [Self kvcdemo4];//kvo demo [self Kvodemo];} -(void) Kvodemo {xnstudent *stu = [[Xnstudent alloc] init];stu.name = @ "Xuneng";//Set Listener Object//1 "Observe the object SELF//2" observed key value path//3 " Observation Options//4 "context: Pass is often used to distinguish between different observation events [Stu addobserver:self forkeypath:@" name "Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:@ "Myobserver"];stu.name = @ "Neng"; Change//All observer mode performance is not good, need to be removed in time [Stu removeobserver:self forkeypath:@ "name"];stu.name = @ "Xu"; Remove and change}/** when the Kvo specified object key value changes, the method name: Observed key value object: Changed dictionary (old and new value) Context: refers to Context */-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (Nsdictionary *) change Context: (void *) Context {NSLog (@ "|--%@--|--%@--|--%@--|--%@--| ", KeyPath, object, change, context);} /** * Simple Change object properties */-(void) KvcDemo1 {//1. The original way. Point syntax changes the value of an object property Xnperson *p1 = [[Xnperson alloc] init];p 1.name = @ "Xuneng";p 1.age = 24;//NSLog (@ "%@,%d", p1.name,p1.age); NSLog (@ "KVC1 demo1-->%@", p1); This calls the description method directly.//2.KVC to change [P1 setvalue:@ "xxxx" forkeypath:@ "name"]; [P1 setvalue:@ "forkeypath:@" "Age"]; NSLog (@ "KVC2 demo1-->%@,%d", P1.name, P1.age);} /** * For subclasses, you can also directly change */-(void) KvcDemo2 {//1. Traditional methods Xnstudent *p1 = [[Xnstudent alloc] init];//Student P1.name = @ "Student Xuneng";p 1. Age = 22;//NSLog (@ "%@,%d", p1.name,p1.age); NSLog (@ "KVC1 demo2-->%@", p1); This calls the description method directly.//2.KVC to change the subclass [P1 setvalue:@ "xxxx" forkeypath:@ "name"]; [P1 setvalue:@ "forkeypath:@" "Age"]; NSLog (@ "KVC2 demo2-->%@,%d", P1.name, P1.age);} /** * Change the book property of the object. (quite troublesome, good to create book object). KVC sentence */-(void) KvcDemo3 {//1. Traditional method Xnstudent *p1 = [[Xnstudent alloc] init];//Student P1.name = @ "Student Xuneng";p 1.age = 22;x Nbook *mybook = [[Xnbook alloc] init]; mybook.bookname = @ "IOS";p 1.book = MyBook; NSLog (@ "KVC1 demo3-->%@", p1),//2.kvc method [P1 setvalue:@ "IPhone" forkeypath:@ "Book.bookname"]; The booknamenslog of the students in the Book,book (@ "KVC2 demo3-->%@", p1);} /** * Gets the property value of the object. Can be obtained in bulk (assuming an array). */-(void) KvcDemo4 {xnstudent *P1 = [[Xnstudent alloc] init];//Student 1p1.name = @ "Student1 Xuneng";p 1.age = 24; Xnbook *mybook1 = [[Xnbook alloc] init];mybook1.bookname = @ "IOS";p 1.book = MyBook1; Xnstudent *P2 = [[Xnstudent alloc] init]; Student 2p2.name = @ "Student2 Xuneng";p 2.age = 23; Xnbook *MYBOOK2 = [[Xnbook alloc] init];mybook2.bookname = @ "IPhone";p 2.book = MyBook2; Nsarray *arr = @[p1, p2];//1. Normal method gets the properties of an object in an array nsmutablearray *arrbook = [[Nsmutablearray alloc] Init];for (xnstudent *stu in ARR) {[Arrbook addObject:stu.book.bookName];} NSLog (@ "KVC1 demo4-->%@", Arrbook);//2.kvc method to get an array of objects NSLog (@ "KVC2 demo4-->%@", [arr valueforkeypath:@] Book.bookname "]);} @end
Sample source code download : http://download.csdn.net/detail/xn4545945/7571719
Reprint Please specify Source: http://blog.csdn.net/xn4545945
"IOS" KVC and KVO