OC Syntax: KVC and KVO, ockvckvo
1. Key-Value Coding (KVC)
KVC refers to NSKeyValueCoding, an informal Protocol that provides a mechanism to indirectly access object attributes. KVO is one of the key technologies based on KVC.
An object has certain attributes. For example, a Person object has a name and an address attribute. In KVC, the Person object has a value corresponding to the key of its name and address. The key is only 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 key value, and the other is to get the key value. Example:
123456789101112 |
void changeName(Person *p, NSString *newName) { // using the KVC accessor (getter) method NSString *originalName = [p valueForKey:@ "name" ]; // using the KVC accessor (setter) method. [p setValue:newName forKey:@ "name" ]; NSLog(@ "Changed %@'s name to: %@" , originalName, newName); } |
Now, if the Person has another key spouse (spouse) and the spouse key value is another Person object, you can write it like this using KVC:
?
12345678910111213 |
void logMarriage(Person *p) { // just using the accessor again, same as example above NSString *personsName = [p valueForKey:@ "name" ]; // this line is different, because it is using // a "key path" instead of a normal "key" NSString *spousesName = [p valueForKeyPath:@ "spouse.name" ]; NSLog(@ "%@ is happily married to %@" , personsName, spousesName); } |
Key and key pat must be distinguished. A key can obtain a value from an object, while a key path can separate multiple keys by the dot ".", for example:
[p valueForKeyPath:@ "spouse.name" ]; |
This is equivalent ......
[[p valueForKey:@ "spouse" ] valueForKey:@ "name" ]; |
Well, the above is the basic knowledge of KVC. Let's take a look at KVO.
Key-Value Observing (KVO)
Key-Value Observing (KVO) is built on KVC. It can observe the change of the key path Value of an object. For example, to observe the address change of a person object using code, the following three methods are implemented:
- WatchPersonForChangeOfAddress: Observed
- ObserveValueForKeyPath: ofObject: change: context: called when the value of the observed key path changes.
- Dealloc stop observation
?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
static NSString * const KVO_CONTEXT_ADDRESS_CHANGED = @ "KVO_CONTEXT_ADDRESS_CHANGED" @implementation PersonWatcher -( void ) watchPersonForChangeOfAddress:(Person *)p { // this begins the observing [p addObserver:self forKeyPath:@ "address" options:0 context: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 will be called - ( void )observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:( void *)context { // use the context to make sure this is a change in the address, // because we may also be observing other things if (context == KVO_CONTEXT_ADDRESS_CHANGED) { NSString *name = [object valueForKey:@ "name" ]; NSString *address = [object valueForKey:@ "address" ]; NSLog(@ "%@ has a new address: %@" , name, address); } } -( void ) dealloc; { // must stop observing everything before this object is // deallocated, otherwise it will cause crashes for (Person *p in m_observedPeople){ [p removeObserver:self forKeyPath:@ "address" ]; } [m_observedPeople release]; m_observedPeople = nil; [super dealloc]; } -(id) init; { if (self = [super init]){ m_observedPeople = [NSMutableArray new ]; } return self; } @end |
This is the role of KVO. It observes the object value through the key path and will receive a notification when the value changes.