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 valueObserver( key values observed ), Viewer Mode . ( Detect model changes with many ) * purpose : typically used to observe that an attribute of an object has changed timely to make corresponding .
Ii. How to use KVC
KVC is called IOS The big trick of developing the platform !!! Can quickly modify object properties.
*[p1setValue: @ "xxxx"forkeypath: @ "name"]; Modify 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 , If the object that is found does not contain the specified key value , is automatically entered into the object's internal members for lookup .
Iii. How to use KVC
* kvo " " , Span style= "COLOR: #000090" to react accordingly ( more for observe 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 many layers (such as Viewcontroller). *KVO Use steps : *1. [Stuaddobserver: selfforkeypath:@ "name" Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:@ "Myobserver"]; *2. Observevalueforkeypath . When the specified key value has changed , This method is called automatically *3. Removeobserver . the performance of all observer patterns is not good , need to be removed in a timely manner.
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 modifies/Gets the properties of the object, reducing the degree of coupling between classes and classes. * Kvo:key value Observer (key value observation) KVO is often used to observe when "an attribute of an object" changes and responds 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 listened to) and the notification hub works! */@implementation xnviewcontroller-(void) viewdidload {[Super VIEWDIDLOAD];//1. Simply modify the object properties [self KVCDEMO1];//2. For subclasses, can also directly modify the [self KVCDEMO2];//3. Modify 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 (if it is 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 " Observed options//4 context: typically used to distinguish between different observation events [Stu addobserver:self forkeypath:@ "name" Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:@ "Myobserver"];stu.name = @ "NENg "; Modify//All observer mode performance is not good, need to be removed in time [Stu removeobserver:self forkeypath:@ "name"];stu.name = @ "Xu"; Remove and modify}/** again when the KVO specified object key value changes, the method is called automatically name: Observed key value object: Observed object change: Modified dictionary (old and new value) context: Specified view The context */-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (Nsdictionary *) change Context: (void *) Context {NSLog (@ "|--%@--|--%@--|--%@--|--%@--|", KeyPath, object, change, context);} /** * Simple modification of object properties */-(void) KvcDemo1 {//1. Original way. Point syntax modifies 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 modify [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 modify the subclass [P1 setvalue:@ "xxxx" forkeypath:@ "name"]; [P1 setvalue:@ "forkeypath:@" "Age"]; NSLog (@ "KVC2 demo2-->%@,%d", P1.name, P1.age);} /** * Modifies the book property of an 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 (if it is 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. GeneralThe Nsmutablearray method gets the properties of the object in the array *arrbook = [[Nsmutablearray alloc] Init];for (xnstudent *stu in arr) {[Arrbook addobject:stu.b Ook.bookname];} NSLog (@ "KVC1 demo4-->%@", Arrbook);//2.kvc method to get an array of objects NSLog (@ "KVC2 demo4-->%@", [arr valueforkeypath:@] Book.bookname "]);} @end
Example source code download : http://download.csdn.net/detail/xn4545945/7571719
Reprint Please specify Source: http://blog.csdn.net/xn4545945