KVO and KVC in iPhone


KVC: Normally instance variables are accessed through properties or accessors but KVC gives another way to access variables in form of strings. In this way your class acts like a dictionary and your property name for example “age” becomes key and value that property holds becomes value for that key. For example, you have employee class with name property.

You access property like

NSString age = emp.age;

setting property value.

emp.age = @”20?;

Now how KVC works is like this

[emp valueForKey:@"age"];

[emp setValue:@"25" forKey:@"age"];

KVO : The mechanism through which objects are notified when there is change in any of property is called KVO.

For example, person object is interested in getting notification when accountBalance property is changed in BankAccount object.To achieve this, Person Object must register as an observer of the BankAccount's accountBalance property by sending an addObserver:forKeyPath:options:context: message.

Ebook Download
View all
Learn
View all