Book Image

Objective C Memory Management Essentials

Book Image

Objective C Memory Management Essentials

Overview of this book

Table of Contents (18 chapters)
Objective-C Memory Management Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Key-value observing


Key-value observing—also known as KVO—is a way to get notified about changes in a variable, but only if it was changed using KVC. We can highlight two things out of this:

  • Firstly, you need KVC in order to do KVO

  • Secondly, if a variable is changed directly without key-value coding by its default setter and getter methods, you won't get notified at all

Every variable in any key path can be observed by an object. It's useful if you consider using KVO. As KVO is built on top of KVC, you need KVC to implement KVO, and using KVO should be one of the reasons why you need to use KVC.

Implementing key-value observing

It is relatively easy to implement KVO, as we shall see in the following code example. On the specified key path, you add an observer. After this, you can create a method that will be called anytime the observer sees modifications in the variables on its key path.

An object can be registered as an observer by using the following method from NSKeyCodingProtocol: addObserver...