O'Reilly logo

Objective-C Programming: The Big Nerd Ranch Guide by Mikey Ward, Aaron Hillegass

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

How KVO works

One example of an Apple API that relies on runtime functions like those above is Key-Value Observing. When you learned about KVO in Chapter Chapter 36, you learned that an observer is automatically notified of a change in a property if the affected object’s accessors are used.

At runtime, when an object is sent the addObserver:forKeyPath:options:context: message, this method:

  • determines the class of the observed object and defines a new subclass of that class using the objc_allocateClassPair function

  • changes the object’s isa pointer to point to the new subclass (effectively changing the type of the object)

  • overrides the observed object’s accessors to send KVO messages

Figure A.1  KVO dynamic subclass

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