KVC introduces a tool for setting values, for objects that point to IDs, like their memory access values. This is a bit like the reflection of Java;
There is an object, known as its property name, that has the desired value:
ID P=[[person alloc] init]; NSString *name=[p valueforkey:@ "name"];
Suppose that there is an array with a series of objects, and we want to take out a property in a queue, as follows:
Book *book1=[[book alloc] init]; [Email protected] "Effctive C + +"; Book *book2=[[book alloc] init]; [Email protected] "more effctive C + +"; Book *book3=[[book alloc] init]; [Email protected] "most effctive C + +"; Nsarray *[email PROTECTED][BOOK1,BOOK2,BOOK3]; NSLog (@ "%@", [arr valueforkey:@ "name"]);
In life, we do exist in the above needs;
KVO refers to the process of setting a value during its monitoring to enable the validation of the business logic
Create a monitored object Book *book1=[[book alloc] init]; Set Listener Object bookobserve *observe=[[bookobserve alloc] init]; Monitoring of its stored value-no need to remove listener after this method is exhausted [observe observevalueforkeypath:@ "name" Ofobject:book1 change:nil context:@ "Sadsada" ]; Monitoring of its stored value -02 [Book1 addobserver:observe forkeypath:@ "name" options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil]; [Email protected] "most effctive C + +"; NSLog (@ "%@", book1.name); -02 [Book1 removeobserver:observe forkeypath:@ "name"] is removed for its stored value;
There is no need to implement any proxy for a listener, because he is a derived class of NSObject
As long as the following methods are implemented in the. m file, you can listen:
#pragma mark KeyPath: Objects Property Object: Object change: Dictionary of Access Values saved-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) Object change: (nsdictionary *) Change context: (void *) context{}