xiv
Contents
Observing Changes to KVC-Compliant Values ................................................................................................................178
Using KVO .........................................................................................................................................................179
Registering as an observer ................................................................................................................................179
Defining callbacks for KVO ................................................................................................................................181
Removing an observer ......................................................................................................................................183
Implementing manual notifications .................................................................................................................184
Understanding risks with KVO ..........................................................................................................................185
Applying Key Value Observing .......................................................................................................................................186
Summary .......................................................................................................................................................................189
Chapter 7: Working with Protocols ....................................191
Favoring Composition Over Inheritance .........................................................................................................................191
Understanding why you don’t need (or want) multiple inheritance .................................................................193
Understanding how protocols solve the problem .............................................................................................194
Documenting an expected interface for others to implement ..........................................................................195
Implementing Protocols in Your Objects........................................................................................................................196
Declaring a protocol .........................................................................................................................................197
Declaring that a class implements a protocol ...................................................................................................198
Declaring an object that must implement a protocol .......................................................................................200
Exploring formal and informal protocols ..........................................................................................................200
Determining if an object implements optional methods ..................................................................................201
Avoiding Circular Protocol Dependencies .........................................................................................................203
Exploring examples of protocol use ...............................................................................................................................204
Summary .......................................................................................................................................................................205
Chapter 8: Extending Existing Class Capabilities ........................207
Working with Third-Party Frameworks and Classes .......................................................................................................207
Working with Categories ...............................................................................................................................................208
Declaring categories .........................................................................................................................................209
Implementing category methods .....................................................................................................................210
Declaring categories in header files ..................................................................................................................211
Using a category ...............................................................................................................................................211
Breaking up functionality using categories .......................................................................................................212
Extending class methods ..................................................................................................................................212
Exploring category limitations ..........................................................................................................................214
Implementing protocols with categories ..........................................................................................................215
Understanding the dangers of creating categories on NSObject .......................................................................215
Extending Classes using Anonymous Categories............................................................................................................216
Associating Variables with Existing Classes ...................................................................................................................218
Summary .......................................................................................................................................................................221
Chapter 9: Writing Macros ............................................223
Reviewing the Compilation Process ...............................................................................................................................223
Understanding how macros work .....................................................................................................................224
Defining Macros ............................................................................................................................................................229
Defining constants............................................................................................................................................230
Passing constants by compilation .....................................................................................................................231
02_9780470479223-ftoc.indd xiv02_9780470479223-ftoc.indd xiv 1/4/11 8:49 PM1/4/11 8:49 PM