iOS Key Value Coding-Key Value Observation

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

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.



Got any iOS Question?