The difference between KVC and KVO

Source: Internet
Author: User

VC is a kind of indirect manipulation of object properties through strings of the mechanism,
Accessing an object property we can person.age also can be KVC by the way [person valueforkey:@ ' age ']
KeyPath is attribute-chained access such as person.address.street a bit like the Pojo OGNL expression in Java

If the given string does not have an object's property to access the Valueforundefinekey method the default implementation is raise an exception but you can rewrite this method, SetValue is also the same reason

Key path Accounts.transactions.payee would return of the payee objects, for all the transactions, in all th E accounts.

Throws an exception when you set an nil property to be an object, but you can also override the method

KVO is an observer pattern that is implemented at the language framework level to actively notify the observer when modifying the attribute in a KVC manner

Example
Person class
@implementation person

@synthesize Name,age;


-(void) changename{
name=@ "ChangeName directly";
}

@end


The Personmonitor class monitors the Name property

@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 notify


Output
2011-07-03 16:35:57.406 cocoa[13970:903] P.name is name
2011-07-03 16:35:57.418 cocoa[13970:903] change happen, Old:name New:name KVC
2011-07-03 16:35:57.420 cocoa[13970:903] p name Get by KVC is name KVC
2011-07-03 16:35:57.421 cocoa[13970:903] Change happen old:name KVC. new:name
The last modification was a direct modification so no notification could be generated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.