How to Set Attributes for an iOS App Declared Property

By Jesse Feiler

You can set certain property attributes when you declare a property for your iOS app. Take a look to discover more details about the attributes you can set when programming.

Setter semantics/ownership

These properties specify how instance variable storage should be managed:

  • strong (similar to retain, which was used previous to ARC) creates an accessor method that means that the object this property points to will be retained while it is in scope (or until it’s set to nil). This is the default value.

  • weak (similar to assign, which was used previous to ARC) creates an accessor that uses simple assignment. You typically use this attribute for scalar types such as NSInteger and CGRect, or (in a reference-counted environment) for objects you don’t own — delegates, for example — and to avoid retain cycle problems.

  • copy specifies that a copy of the object should be used for assignment. The previous value is sent a release message.

    The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.

For object properties, you must explicitly specify one of the types listed previously; otherwise, you get a compiler warning. So you need to think about what memory management behavior you want, and type the behavior explicitly.

Writability

The following attributes specify whether a property has an associated set accessor. They are mutually exclusive.

  • readwrite indicates that the property should be treated as read/write. This attribute is the default. The getter and setter methods are synthesized automatically.

  • readonly indicates that the property is read-only. Only a getter method is synthesized. If you implement your own accessors, only a getter method is required. If you attempt to assign a value using the dot syntax, you get a compiler error.

Accessor method names

You’ll remember that the default names for the getter and setter methods associated with a property are PropertyName and setPropertyName:, respectively. For example, for the property trip, the accessors are trip and setTrip:. You can, however, specify custom names instead. They’re both optional and can appear with any other attribute (except for readonly and in the case of setter=):

  • getter=getterName specifies the name of the get accessor for the property. The getter must return a type matching the property’s type and take no parameters.

  • setter=setterName specifies the name of the set accessor for the property. The setter method must take a single parameter of a type matching the property’s type and must return void.

Typically, you should specify accessor method names that are key-value coding compliant. A common reason for using the getter decorator is to adhere to the isPropertyName convention for Boolean values.

If you have a Boolean property called alphabetized that you use to keep track of how an array of names is sorted, the default getter would be alphabetized. Using the naming convention, you would create an accessor method called isAlphabetized, which is a bit clearer.

Atomicity

You can use this attribute to specify that accessor methods aren’t atomic. (No keyword denotes atomic.) This has to do with concurrency issues. If you specify nonatomic, a synthesized accessor for an object property simply returns the value directly. Otherwise, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value.