Chapter 28: Deep Objective-C
Much of Objective-C is very straightforward in practice. There is no multiple inheritance or operator overloading like in C++. All objects have the same memory-management rules, which rely on a simple set of naming conventions. With the addition of ARC, you don’t even need to worry about memory management in most cases. The Cocoa framework is designed with readability in mind, so most things do exactly what they say they do.
Still, many parts of Objective-C appear mysterious until you dig into them, such as creating new methods and classes at runtime, introspection, and message passing. Most of the time, you don’t need to understand how this works, but for some problems, it’s very useful to harness the full power of Objective-C. For example, the flexibility of Core Data relies heavily on the dynamic nature of Objective-C.
The heart of this power is the Objective-C runtime, provided by libobjc
. The Objective-C runtime is a collection of functions that provides the dynamic features of Objective-C. It includes such core functions as objc_msgSend
, which is called every time you use the [object message]
syntax. It also includes functions to allow you to inspect and modify the class hierarchy at runtime, including creating new classes and methods.
This chapter shows you how to use these features to achieve the same kind of flexibility, power, and speed as Core Data and other Apple frameworks. All code samples in this chapter can be found in the online ...