Key Value Coding-Key Value Observation
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.
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.
Observing a property of a NSObject subclass
Most KVO and KVC functionality is already implemented by default on all
To start observing a property named
firstName of an object named
personObject do this in the observing class:
The object that
self in the above code refers to will then receive a
observeValueForKeyPath:ofObject:change:context: message whenever the observed key path changes.
"Key path" is a KVC term.
NSObject subclasses implement KVC functionality by default.
An instance variable named
_firstName will be accessible by the
@"firstName" key path.
A getter method named
firstName will be called when accessing the
@"firstName" key path, regardless of there being a
_firstName instance variable or
setFirstName setter method.
Use of context for KVO Observation
Context is important if you ship your class for others to use.Context lets your class observer verify that its you observer which is being called.
The problem with not passing an observer is, if some one subclass your class and register an observer for the same object,same key and he does not passes a context ,then the super class observer can be called multiple time.
A variable which is unique and internal for your use is a good context.
For more information.