Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The main focus of this chapter has been how to set and retrieve the values stored in an object’s instance variables using the new Objective-C 2.0 declared property feature. These are the main points to remember about accessing instance variables and using properties:
You should access an object’s instance variables by using accessor methods to get and the instance variable’s value. You shouldn’t access the instance variables directly from outside the object.
The @property statement lets you declare the accessor methods in a convenient shorthand form.
Property declarations provide extra information about the accessor methods such as whether object instance variables are assigned, retained, or copied, and whether the accessors use locks to ensure atomicity.
When accessors are declared with a property declaration, you can ask the compiler to create the code for the accessor methods for you by using a @synthesize directive. The synthesized accessor reflects the attributes specified in the @property statement.
Using @synthesize creates very basic accessors that only set or retrieve the property value. If you need to do extra processing when accessing a property (for example, clamping an input value to a specified range), you can omit the @synthesize statement (or use @dynamic) and code the accessor for the property yourself.
Objective-C 2.0 also introduces the dot syntax, a shorthand way to call accessor methods.