When you call an object through KVC, for example, [self valueforkey: @ "somekey"], the program automatically attempts to parse the call in several different ways. First, check whether the object has the somekey method. If it is not found, it will continue to find whether the object has the somekey instance variable (Ivar). If it has not been found, the program will continue to try to call-(ID) valueforundefinedkey: This method. If this method is still not implemented, the program will throw an nsundefinedkeyexception error.
KVC is a mechanism to indirectly manipulate object attributes through strings,
To access an object property, we can use person. Age or KVC [person valueforkey: @ "Age"]
Keypath is the attribute chain access, such as person. Address. Street, which is somewhat like the pojo ognl expression subclass in Java.
If the given string has no object attribute, the valueforundefinekey method will be accessed. The default implementation is raise. However, you can rewrite this method. The same applies to setvalue.
Key Path accounts. Transactions. Payee wocould return an array with all the payee objects, for all the transactions, in all the accounts.
An exception is thrown when a non-object attribute is set to nil, but you can override the method.
KVO is an observer mode implemented at the language framework layer. When you modify attributes using KVC, KVO actively notifies the observer.
Example
Person class
@ Implementation person
@ Synthesize name, age;
-(Void) changename {
Name = @ "changename directly ";
}
@ End
The personmonitor class monitors the name attribute.
@ Implementation personmonitor
-(Void) observevalueforkeypath :( nsstring *) keypath
Ofobject :( ID) object
Change :( nsdictionary *) Change
Context :( void *) Context
{
If ([keypath isequal: @ "name"]) {
Nslog (@ "change happen, old: % @ New: % @", [Change objectforkey: nskeyvaluechangeoldkey], [Change objectforkey: nskeyvaluechangenewkey]);
}
}
@ End
Test code
Person * P = [[person alloc] init];
Personmonitor * PM = [[personmonitor alloc] init];
[P addobserver: PM forkeypath: @ "name" Options :( nskeyvalueobservingoptionnew |
Nskeyvalueobservingoptionold) Context: Nil];
Nslog (@ "P. Name is % @", p. Name );
[P setvalue: @ "name KVC" forkey: @ "name"];
Nslog (@ "P name get by KVC is % @", [p valueforkey: @ "name"]);
P. Name = @ "name change by. Name = ";
[P changename]; // can't be named y
Output
16:35:57. 406 cocoa [13970: 903] P. Name is name
16:35:57. 418 cocoa [13970: 903] Change Happen, old: Name New: Name KVC
16:35:57. 420 cocoa [13970: 903] P name get by KVC is name KVC
16:35:57. 421 cocoa [13970: 903] Change Happen, old: Name KVC New: Name Change by. Name =
The last modification is direct modification, so no notification can be generated.