Objective-C: Class Property Attribute Types

Published on Christian Mayer's Weblog

@propertys 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

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

Synthesizing / Ownership

Attr Synt Own
atomic    
nonatomic    
readwrite    
readonly    
assign YES  
retain YES YES
copy    
strong   YES
weak   YES
unsafe_unretained   YES
Legend

Sources

iDhaval on StackOverflow:

Brad Larson on StackOverflow:

To target the older OS, you can use unsafe_unretained instead of weak in your property declaration, and it should mostly work the same way. weak references nil themselves when their target goes away, but unsafe_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 with weak. This will be much easier than searching for assign and figuring out if you actually meant assign or weak

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 use YES instead of 1.

Footnotes

More Resources

Recent Posts

About the Author

Christian is a professional software developer living in Vienna, Austria. He loves coffee and is strongly addicted to music. In his spare time he writes open source software. He is known for developing automatic data processing systems on Debian Linux server.

Categories: Productivity, Programming
Tags: Mac, MacOSX, OS X, OSX, OS, X, Apple, Objective-C, ObjectiveC, ObjC, ARC, Automatic Reference Counting, Memory Management

Archive | Categories | RSS Feed | Usage | Imprint
Copyright © 2006 by