Properties (iOS 4)

What we’ve covered so far should be sufficient for you to understand (and write) most simple Objective-C code. One other major feature in Objective-C deserves some extended discussion because of its unique syntax: the property.

The purpose of properties

Because instance variables are encapsulated, you usually have to write tons of getter and setter methods when doing OOP. This can get tedious, and you must also be careful about consistency so you don’t have dozens of different syntaxes for your accessors and mutators.

Objective-C offers a solution to these problems: you can declare an instance variable as a property. When you do so, you standardize the variable’s accessor and muta-tor methods by automatically declaring a getter and a setter. The setter is called setVariable and the getter is called variable.

For example, returning to the apples that we’ve been talking about in our major examples, if you define the NSString *appleType; variable as a property, the following declarations automatically occur:


You’ll never see these declarations, but they’re there.

Setting a property

You declare an instance variable as a property by using the @property directive as part of your @interface statement. The following listing demonstrates how to do so, in the full context of the example so far.

Listing 2.3 AppleTree.h

Listing 2.3 AppleTree.h

The header file shows that any property must start with the declaration of an instance variable. The @property directive then repeats that declaration. If you wish, you can stop here. You’ve now implicitly declared your accessor and mutator methods, and you can go and write those methods on your own if you see fit. Let’s look at another example.

Listing 2.4 AppleTree.m

Listing 2.4 AppleTree.m

Objective-C will also write these methods for you if you ask it to. This is done with the @synthesize declaration in the ¬©implementation statement. This creates accessor methods that read and set the variable by the simple methods you’d expect. The setter method is by default of type assign, but you can choose a different method using property attributes, which we’ll talk about down the road.

Using the accessors

If you’re not doing anything fancy, you can immediately use your class’s default getter and setter methods, as shown in the following three examples:


In addition to providing you with automatically created accessors and mutators, properties also give you access to a bit of syntactic sugar, which can make using them that much easier.

The dot syntax

Objective-C offers a dot syntax that makes it easy to use an object’s accessor and muta-tor methods (whether you synthesized them or created them yourself). The following are the dot-syntax equivalents to the messages you sent earlier:


The dot syntax can also be nested, just as you can nest messages. In the following example, the treeType property returns a tree object that has an AppleType property:


With that in hand, you should now be able to write simpler and more intuitive code.

Property complexities

There are several complexities of properties that we’ve opted not to delve into here. First, property declarations can include attributes. They let you change getter and setter names, change setter assignment methods, modify memory management (retain, autorelease, and so on), set nonatomic accessors (which are accessors that can be interrupted by the CPU scheduler while in use), and determine whether the property is read-only or read-write. These can all be set as part of the @property line.

Second, another directive called @dynamic lets you add accessor and mutator methods at runtime.

Third, it’s possible to override default values that you’ve synthesized through normal method creation as part of your @implementation.

A variety of information about properties is available in Apple’s Objective-C reference; if you need to delve into any of these complexities, you should refer to that.

Next post:

Previous post: