Learning Objective-C 2.0: A Hands-on Guide to Objective-C for Mac and iOS Developers, Second Edition
by Robert Clair
12. Properties
One of the most common programming tasks is to set or retrieve a value held in one of an object’s instance variables. Although it is possible in some cases to access the instance variable directly, doing so is a bad practice. The correct way to access an instance variable is to write accessor methods. An accessor method that sets an instance variable’s value is called a setter, and one that returns an instance variable’s value is called a getter.
For example, if your object has the integer instance variable numberOfDogs, you should declare and implement the following two methods:
// returns the current value-(NSInteger) numberOfDogs; // sets a new value-(void)setNumberOfDogs:(NSInteger) newNumberOfDogs; ...
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.
Read now
Unlock full access