O'Reilly logo

Core Data in Swift by Marcus S. Zarra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Key Value Observing

Key Value Observing (KVO) is the sister API to KVC. KVO allows us to request notifications when an attribute has changed. By observing attributes on an object, we can react when those attributes are changed. KVO is also implemented via an informal protocol on the NSObject, and we register and remove observers using addObserver(: forKeyPath: options: context:) and removeObserver(: forKeyPath:). These are the two primary methods, although there are other methods involved in the protocol, just as with KVC. If we wanted to observe the name value on a recipe, we’d add ourselves (or another object) as an observer for that value, like so:

 let​ kPragProgObserver = ​"PragProgObserver"
 let​ myRecipe = ...
 myRecipe.addObserver(​ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required