First, Key-value Coding (KVC) key value code
KVC, which means nskeyvaluecoding, an informal Protocol, provides a mechanism to indirectly access the properties of an object. KVO is one of the key technologies based on KVC implementation.
An object has some properties. For example, a person object has a name and an age property. In KVC parlance, the person object has a value corresponding to the key of his name and age. A key is just a string, and its corresponding value can be any type of object. At the most basic level, KVC has two methods: one is to set the value of key and the other is to get the value of key. As in the following example:
// using the KVC accessor (getter) Method-valueforke NSString *personlname = [P Valueforkey:@ "name"]; // using the KVC accessor (setter) Method.-setvalue:forkey: [P setvalue:newname Forkey:@ "name"];
Now, if person has another key spouse (spouse), spouse's key value is another person object, which can be written in KVC:
// just using the accessor again, same as example above NSString *personsname = [P Valueforkey:@ "name"]; // This was different, because it is using // a "key path" instead of a normal "key" NSString *spousesname = [P Valueforkeypath:@ "spouse.name"];
Valueforkey and Valueforkeypath to distinguish between, Valueforkey can get the value from an object, and Valueforkeypath can be a number of keys with the dot "." Split, such as:
[p valueForKeyPath:@
"spouse.name"
];等同于[[p valueForKey:@
"spouse"
] valueForKey:@
"name"
];
Second, Key-value observing (KVO) key value observation
Key-value Observing (KVO) is built on the KVC, and it is able to observe the change of the KVC key path value of an object. For example, using code to observe the age change of a person object, here are three ways to implement it:
- Watchpersonforchangeofaddress: Realization Observation
- ObserveValueForKeyPath:ofObject:change:context: Called when the value of the observed key path changes.
- Dealloc Stop Observation
StaticNSString *ConstKvo_context_address_changed =@"kvo_context_address_changed"@implementation Personwatcher-(void) Watchpersonforchangeofaddress: (Person *) p{//This begins the observing to add a key-value listener event[P addobserver:self forkeypath:@"Address"Options:0context:kvo_context_address_changed]; //keep a record of all the people being observed,//because we need to stop observing them in Dealloc[M_observedpeople addobject:p];} //whenever an observed key path changes, this method would be called- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange : (Nsdictionary*) Change context: (void*) Context {//Use the context to make sure-a change in the address,//because we may also is observing other things-key value changes when the corresponding operation if(Context = =kvo_context_address_changed) {NSString*name = [ObjectValueforkey:@"name"]; NSString*address = [ObjectValueforkey:@"Address"]; NSLog (@"%@ has a new address:%@", name, address); }} -(void) Dealloc; { //must stop observing everything before this object is//deallocated, otherwise it will cause crashes-remove the key-value listener when destroying for(Person *pinchm_observedpeople) {[P removeobserver:self forkeypath:@"Address"]; } [M_observedpeople release]; M_observedpeople=Nil; [Super Dealloc]; }
This is the function of KVO, which is notified when the value of the object is changed by key path.
KVC and KVO