Using KVO to know what has been updated
Key Value Observing (KVO) is the sister API to KVC and is used to inform us if a particular attribute of an object is changed. Consequently, we can take necessary action on getting notification of the change(s) that took place on any attribute of the object. To get the notification, we have to register our controller class (referred to as self) as an observer of the object for the keypath, where keypath is the name of the attribute of the object that we want to observe.
To register the observer, we use -addObserver:forKeyPath:options:context:
method and to remove the observers, we use -removeObserver: forKeyPath:
method.
Let us have a look at the syntax of the method that registers an observer:
[object addObserver:self forKeyPath:@"attribute" options:0 context:NULL];
Here, object
is the name of object whose keypath (attribute) we want to observe. In order to observe the name
attribute of the cust
object (of the Customer
class), we may add an observer...