Start a Project

KVO and KVC in Swift

We all need variables or properties to store any values in them. The navigation of the program depends on the values stored in the defined variables. What happens when the values in the defined data structure change? We have to implement the logic for any values in the variable. Here comes the role of the KVO and KVC in Swift.

Introduction to KVO and KVC  in Swift

Changes in the properties can be observed using Notification-Centre also.

We can send a notification to notify an observing property about the change and perform the actions based on the value that was changed.

Using Notification Centre for a large number of properties will lead to a large number of notifications to send and thus making the code bulkier. To avoid this we use KVO(Key-Value Observing).

KVO is directly related to the  KVC(Key-Value Coding)

First, we need to understand what is KVC?

 

KVC in Swift

According to Apple:

Key-value coding is a mechanism for accessing an object’s properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables.

NSObject confirms to NSKeyValueCoding protocol, so any class that is inherited from NSObject directly conforms to the NSKeyValueCoding protocol.

KVC is generally a coding mechanism that we follow to get or set any property values. The working of the KVC is more like the Dictionaries.

From the below example it will be clear how to use KVC.

We would generally initialize the properties in DemoClass like

With KVC we follow the below syntax to set and retrieve values of the properties.

self.setValue: Any for key: key/KeyPath

For retrieving values :

Here we are setting and getting the values in property using the Key. We can set or get the values by KeyPath also.

KeyPath represents the absolute path to the property.

Using dot-syntax we can reach out to the property.

KVO in Swift

In KVO we observe the change in the values of the properties. This can be achieved using NSNotifications.

We have to keep in mind some steps before using KVO, these are:

  1. The class in which you are implementing the  KVO must be able to send notifications.
  2.  If the implementing class is inheriting from the NSObject it is automatically KVC compliant.
  3. The observing class must be specified as the observer.

Note: Add the observer for the observing property inside the viewDidLoad() or viewWillAppear() method.

Above mentioned parameters are:

  1. addObserver: it is the observing class it is generally self(refers to the class in which you are).
  2. forKeyPath: as described earlier defines the path to the property.
  3. options: it is an array of the NSKeyValueObservingOptions.
  4. context: it is a pointer that can be used as an identifier for the change in the property. You can set it to nil also.

We have to implement the below function in order to observe the change in the property

Inside this method, we can perform any logic for any keyPath.

The main disadvantage of this method is that we have to check the change in the values for each key/keyPath.

The best example of KVO and KVC is how UserDefaults uses this mechanism to set, change and modify the values.

Please click here to know how Apple describes KVO and KVC

 

Thanks for reading!!

For other blogs Please click here

Exit mobile version