@property
s are defined in @interface
of a class.
For example:
@property (nonatomic) NSString *name;
Here is a list of property attributes compared to the operating system.
OS X
Consider that ARC is available since
- Mac OS X 10.6
- and iOS 4.0.
Without ARC using clang on x86_64
Attr \ OS | 10.5 | 10.6 | >=10.7 |
---|---|---|---|
atomic |
D (X) | ~ | ~ |
nonatomic |
? | ~ | ~ |
readwrite |
D | ~ | ~ |
readonly |
? | ~ | ~ |
assign |
D (!) | ~ | ~ |
retain |
(!) | ~ | ~ |
copy |
(!) | ~ | ~ |
strong (=retain ) |
? | ~ | |
weak (=assign ) |
|||
unsafe_unretained (=weak ) |
? | ~ | |
@synthesize |
(!) | ~ | D |
@dynamic |
(!) | ~ | D |
With ARC using clang on x86_64
Attr \ OS | 10.6 | >=10.7 |
---|---|---|
atomic |
D | ~ |
nonatomic |
? | ~ |
readwrite |
D | ~ |
readonly |
? | ~ |
assign |
D (!) | ~ |
retain |
(!) | ~ |
copy |
(!) | ~ |
strong (=retain ) |
? | ~ |
weak (=assign ) |
? | |
unsafe_unretained (=weak ) |
? | ~ |
@synthesize |
(!) | D |
@dynamic |
(!) | D |
Legend
D
- default.(D)
default with exceptions. [1]!
- required.(!)
another required option. [2]X
- forbidden.(X)
forbidden on GNU. [3]?
- available.(?)
available but no effect.~
- not changed since previous version.- Space - not available
Synthesizing / Ownership
Attr | Synt | Own |
---|---|---|
atomic |
||
nonatomic |
||
readwrite |
||
readonly |
||
assign |
YES | |
retain |
YES | YES |
copy |
||
strong |
YES | |
weak |
YES | |
unsafe_unretained |
YES |
Legend
Synt
- properties for synthesizing the property.Own
- properties for ownership.
Sources
iDhaval on StackOverflow:
-
Nonatomic -
nonatomic
is used for multi threading purposes. If we have set the nonatomic attribute at the time of declaration, then any other thread wanting access to that object can access it and give results in respect to multi-threading. -
Copy -
copy
is required when the object is mutable. Use this if you need the value of the object as it is at this moment, and you don’t want that value to reflect any changes made by other owners of the object. You will need to release the object when you are finished with it because you are retaining the copy. -
Assign -
assign
is somewhat the opposite tocopy
. When calling the getter of anassign
property, it returns a reference to the actual data. Typically you use this attribute when you have a property of primitive type (float, int, BOOL…) -
Retain -
retain
is required when the attribute is a pointer to an object. The setter generated by@synthesize
will retain (aka add a retain count to) the object. You will need to release the object when you are finished with it. By using retain it will increase the retain count and occupy memory in autorelease pool. -
Strong -
strong
is a replacement for theretain
attribute, as part of Objective-C Automated Reference Counting (ARC). In non-ARC code it’s just a synonym for retain. This is a good website to learn aboutstrong
andweak
for iOS 5. http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1 -
Weak -
weak
is likestrong
except that it won’t increase the reference count by 1. It does not become an owner of that object but just holds a reference to it. If the object’s reference count drops to 0, even though you may still be pointing to it here, it will be deallocated from memory. The above link contain both Good information regardingweak
andstrong
.
Brad Larson on StackOverflow:
To target the older OS, you can use
unsafe_unretained
instead ofweak
in your property declaration, and it should mostly work the same way.weak
references nil themselves when their target goes away, butunsafe_unretained
leaves open the possibility that the object you’re linking to could turn into a dangling pointer when it is deallocated. The latter is the same behavior as if you had used assign as a property declaration in manual memory management.
Matthias Bauch on StackOverflow:
if so, which one to prefer when in iOS4, or why there is (unsafe_unretained) if its exactly same as assign? you should use
unsafe_unretained
. You want to show the reader of your code that you actually wanted to use weak but that this was not possible because weak is not available on the iOS version you want to deploy.One day you will drop the support for iOS4. And then you could just search for
unsafe_unretained
and replace all of them withweak
. This will be much easier than searching forassign
and figuring out if you actually meantassign
orweak
The use of
unsafe_unretained
creates more readable and understandable code where the intentions of the developer are easier to see. Basically the same reason we useYES
instead of1
.
Footnotes
- [1] Under ARC,
strong
is the default for object types. See Transitioning to ARC Release Notes » ARC Introduces New Lifetime Qualifiers » Property Attributes. - [2] If you don’t use of one the required options you get a warning like this:
warning: no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed
warning: property 'name_310' requires method 'name_310' to be defined - use @synthesize, @dynamic or provide a method implementation in this class implementation
- [3] You get an error like this:
error: expected a property attribute before 'atomic'