Native Shopify Mobile App with 20 new features
Kickstart your hyperlocal marketplace in Corona pandemic with a starter guide
Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 22.214.171.124 release ready!
Owner and Founder. Opencart
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.
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?
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.
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:
Note: Add the observer for the observing property inside the viewDidLoad() or viewWillAppear() method.
Above mentioned parameters are:
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
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.