Synthesized Accessor Methods

As of Objective-C 2.0, you can have your setter and getter methods (collectively known as accessor methods) automatically generated for you. We haven’t shown you how to do this up to this point because it was important for you to learn how to write these methods on your own. However, it’s a nice convenience provided in the language, so it’s time for you to learn how to take advantage of this feature.

The first step is to use the @property directive in your interface section to identify your properties. These properties are often named the same as your instance variables, although they don’t have to be. In the case of our Fraction class, the two instance variables numerator and denominator fall into this category. ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.