Properties

Properties let you define instance variables and let the compiler take care of creating the accessor methods—that is, methods that access (get or set) variables or information. The compiler also generates the underscore version of the variable. Declaring a property is simple:

@property float fuelAmount;

This tells the compiler to create one instance variable and two methods:

float _fuelAmount; - (float)fuelAmount; - (void)setFuelAmount:(float)fuelAmount;

You might have noticed that the variable and method declarations are identical to the ones in Listing 2-3.

The compiler generates the underscore version of the variable for you. Any non-car objects have to use the getter and setter. The variables and method ...

Get Learning iOS Development: A Hands-on Guide to the Fundamentals of iOS Programming 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.