01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Declared properties<br />

111<br />

Let’s briefly look at how to use the optional @property attributes listed in table 5.2 to<br />

alter the behavior of the declared properties.<br />

METHOD NAMING<br />

By default for a property named foo, the <strong>Objective</strong>-C compiler produces a getter<br />

method named foo and a setter method named setFoo:. You can override these names<br />

by explicitly specifying an alternative name via the optional getter and setter attributes.<br />

As an example, the following property declaration provides a getter called<br />

isSelected and a setter called setSelected:<br />

@property (getter=isSelected) BOOL selected;<br />

This attribute is commonly used with properties with a BOOL data type to rename their<br />

getter methods into the form isXYZ, another <strong>Objective</strong>-C naming convention.<br />

WRITEABILITY<br />

A property typically indicates the presence of both a getter and setter method. By<br />

adding the readonly attribute to a property declaration, you can ensure that users of<br />

the class can only query the current value of the property and can’t change its value.<br />

For example, the following declaration specifies that the age property has only a getter<br />

method:<br />

@property (readonly) int age;<br />

The default behavior of each property having a getter and setter method can also explicitly<br />

be specified using the readwrite attribute. Because a readonly property has no setter,<br />

code in the class would have to modify the associated instance variable directly.<br />

SETTER SEMANTICS<br />

These attributes enable you to specify how a setter method will deal with memory<br />

management. Following are the three mutually exclusive options:<br />

■<br />

■<br />

■<br />

Assign—The setter uses a simple assignment statement (the default option).<br />

Retain—The setter calls retain on the new value and release on the old one.<br />

Copy—A copy of the new value is made and retained.<br />

Memory management can become a complex topic (look out for chapter 9). For now,<br />

it’s enough to understand that assign indicates that no additional memory management<br />

behavior is required (the property assignment is treated as if it were a simple<br />

variable). The copy attribute requests that a “carbon-copy” duplicate of the value<br />

being provided be made and the copy stored, and the retain attribute does something<br />

in between.<br />

THREAD SAFETY<br />

By default, properties are atomic. This is fancy-speak for indicating that in a multithreaded<br />

environment, <strong>Objective</strong>-C ensures the value obtained via a getter or set via a<br />

setter is always consistent and not corrupted by concurrent access by other threads.<br />

This protection, however, doesn’t come for free and may have a performance cost<br />

associated with it due to the time taken to acquire the locks needed to protect against

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!