Objective C Property Qualifiers
In addition to readwrite and readonly, you can specify whether a property is retained
and/or atomic.The default behavior for properties in ARC is strong; in MRR the
default behavior is assign. Strong and retain are synonymous;“strong” emphasizes the
object relationship while “retain” places its emphasis on the underlying mechanics.
Assigned properties are not retained.ARC uses two styles of unretained property
assignment.A weak property uses self-nullifying pointers; you never have to worry about
dangling pointers.An unsafe_unretained property simply points to memory and, as its
name indicates, may point to an unsafe address. Under ARC, assign properties are used
to point to non-object value types such as integers and structures.
The following sections discuss property qualifiers for MRR and ARC compilation.
MRR Qualifiers
With assign, there’s no special retain/release behavior associated with the property, but
by making it a property you expose the variable outside the class via dot notation. In
MRR, a property that’s declared
@property NSString *make;
uses the assign behavior.
Setting the property’s attribute to MRR’s retain does two things. First, it retains the
passed object upon assignment. Second, it releases the previous value before a new assignment
is made.You can clear up any current memory usage by assigning the retained property
to nil.To create a retained property, add the attribute between parentheses in the
declaration:
@property (retain) NSString *make;
A third attribute called copy copies the passed object and releases any previous value.
With MRR, copies are always created with a retain count of 1.
@property (copy) NSString *make;
ARC Qualifiers
When you’re declaring properties, strong properties automatically retain the objects
assigned to them, releasing them only when the object whose property this is gets released
or the property is set to nil. Use strong properties to hold onto any items that may be
referenced through the lifetime of your object. By setting a property to be strong, you’re
assured that the instance the property points to will not be released back into the general
memory pool until you’re done with it, as shown here:
@property (nonatomic, strong) NSDate *date;
Like MRR’s assign properties, weak properties do not retain objects or otherwise
extend their lifetime. However, weak properties do something that assign properties
never did.They ensure that if the object being pointed to gets deallocated, the property
returns nil, not a reference to reclaimed memory.ARC eradicates dangling pointers, creating
safe nil values instead.This is known as “zeroing” weak references.
Use assign properties in ARC to point to items that aren’t objects, such as floats and
Booleans as well as structs such as CGRect:
@property (assign) CGRect bounds;
Atomic Qualifiers
When you develop in a multithreaded environment, you want to use atomic properties.
Xcode synthesizes atomic properties to automatically lock objects before they are accessed
or modified and unlock them after.This ensures that setting or retrieving an object’s valueis performed fully regardless of concurrent threads.There is no atomic keyword.All methods
are synthesized atomically by default.You can, however, state the opposite, allowing
Objective-C to create accessors that are nonatomic:
@property (nonatomic, strong) NSString *make;
Marking your properties nonatomic does speed up access, but you might run into
problems should two competing threads attempt to modify the same property at once.
Atomic properties, with their lock/unlock behavior, ensure that an object update completes
from start to finish before that property is released to a subsequent read or change.
Some will argue that accessors are not usually the best place for locks and cannot
ensure thread safety.An object might be set to an invalid state, even with all atomic properties.
As Jay Spenser, one of my early readers, pointed out,“If you had a trade-in thread and
an inventory thread, you could end up thinking you had a 1946 Tesla Prefect on your lot.”

浙公网安备 33010602011771号