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

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.