Chapter 12. Accessors and Memory Management
This chapter returns to three aspects of Objective-C introduced briefly in Chapter 5 — accessors, key–value coding, and properties — discussing them more deeply and rigorously, and describing in particular the special role that they play in one of the most important and crucial aspects of Cocoa: memory management of Objective-C instances.
An accessor is a method for getting or setting the value of an instance variable. An accessor that gets the instance variable’s value is called a getter; an accessor that sets the instance variable’s value is called a setter.
Accessors are important in part because instance variables, by default, are protected (Chapter 5), whereas publicly declared methods are public; without public accessor methods, a protected instance variable can’t be accessed by any object whose class (or superclass) isn’t the one that declares the instance variable.
You might be tempted to conclude from this that you needn’t bother making an accessor for an instance variable that isn’t intended for public access, and to some extent this is a reasonable conclusion. However, in modern Objective-C, making accessors is as easy as declaring an instance variable: you declare a property, and the instance variable along with the accessors come into existence automatically — you don’t have to write any accessor code. Plus, if you do write accessor code, you can consistently perform additional tasks when the instance variable’s value ...