Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding KVO in iOS

Regarding "Ensuring KVO Compliance", there are some official definition which seem like hard to understand

In order to be considered KVO-compliant for a specific property, a class must ensure the following;

  1. The class must be key-value coding compliant for the property as specified in Ensuring KVC Compliance.

  2. The class must allow automatic observer notifications for the property, or implement manual key-value observing for the property.

Who can give more specific examples to make this more clear ? Thanks

like image 463
Forrest Avatar asked Jan 27 '11 06:01

Forrest


People also ask

What is KVO in iOS?

Key-value observing is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It's useful for communicating changes between logically separated parts of your app—such as between models and views. You can only use key-value observing with classes that inherit from NSObject .

Can you explain KVO?

KVO, which stands for Key-Value Observing, is one of the techniques for observing the program state changes available in Objective-C and Swift. The concept is simple: when we have an object with some instance variables, KVO allows other objects to establish surveillance on changes for any of those instance variables.

What is KVO and KVC in iOS Swift?

KVO and KVC or Key-Value Observing and Key-Value Coding are mechanisms originally built and provided by Objective-C that allows us to locate and interact with the underlying properties of a class that inherits NSObject at runtime.

How does KVO work Swift?

Key-value observing is a mechanism that enables an object to be notified directly when a property of another object changes. It is a mode of communication between objects in applications designed in conformance with the Model-View-Controller design pattern.


2 Answers

Take a look at Ensuring KVO Compliance the Automatic Versus Manual Support section of the Key-Value Observing Programming Guide. Compliance is essentially achieved by following naming conventions for methods and/or ivars.

In my experience KVO tends to 'just work', which is nice.

like image 157
Benedict Cohen Avatar answered Sep 28 '22 10:09

Benedict Cohen


When you use @property tags to create getters/ setters the magic is auto wired for To-One relationships and you only need to addObserver... and implement the observe... method to catch the updates.

The real challenge is understanding how you implement To-Many Key Value Compliance to make a mutable set or array work. The documentation here is key and the understanding that there are two sections... Indexed collections and Unordered collections. It really helped me to understand that if my @property is an NSMutableArray you need to look at the methods in the Indexed area and if your @property is a NSMutableSet you need to look at the Unordered documentation.

There are Required methods that you need to implement even if you don't plan to use them.

like image 27
Samuel Avatar answered Sep 28 '22 10:09

Samuel