KVO and KVC in iPhone

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

You access the 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 a change in any of properties; it is called KVO.

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


Similar Articles