Skip to Content
iOS 7 Programming Fundamentals
book

iOS 7 Programming Fundamentals

by Matt Neuburg
October 2013
Beginner content levelBeginner
422 pages
12h 35m
English
O'Reilly Media, Inc.
Content preview from iOS 7 Programming Fundamentals

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.

Accessors

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

iOS 7 Programming Cookbook

iOS 7 Programming Cookbook

Vandad Nahavandipoor
Programming iOS 11

Programming iOS 11

Matt Neuburg
Programming iOS 12

Programming iOS 12

Matt Neuburg

Publisher Resources

ISBN: 9781491946039Errata PageSupplemental Content