KVC and KVO are the key concepts of Objective C, something that the individual believes must be understood, and the following is an example explanation.
Key-value Coding (KVC)
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 address property. In KVC parlance, the person object has a value corresponding to the key of his name and address. 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:
?
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 person has another key spouse (spouse), spouse's key value is another person object, which can be written in 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 to distinguish, key can get the value from an object, and key path can be more than one key with the dot "." Split, such as:
[p valueForKeyPath:@ "spouse.name" ]; |
The equivalent of this ...
[[p valueForKey:@ "spouse" ] valueForKey:@ "name" ]; |
Well, the above is the basic knowledge of KVC, then look at KVO.
Key-value Observing (KVO)
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 address 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
?
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 function of KVO, which is notified when the value of the object is changed by key path.
KVC and KVO Understanding