Key Value Coding-Key Value Observation

Download ios eBook


KVC :- Key-Value Coding

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 "age" property. Normally we access like this.
emp.age = @”20″;
NSString age = emp.age; 

But KVC works like this: 
[emp valueForKey:@"age"]; 
[emp setValue:@"25" forKey:@"age"];

KVO :- Key-Value Observer

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

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.

Related Examples


128 Contributors: 8
Wednesday, May 24, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook