Attributes of @property : nonatomic, retain, strong, weak etc..

This article is intended to be a reference guide to the property attributes.

By default @property looks like this:

@property(atomic,readwrite,strong)NSString *name;


@property(atomic,readwrite,assign)NSInteger age;

Access Attributes:

readonly : As the name suggests, value cannot be assigned to the object. It omits the setter method.

readwrite : Value of object can be assigned as well as retrieved from other object.

Threading Attribute:

atomic : Declaring a property atomic makes compiler generate additional code that prevents concurrent access to the object by multiple threads.

This additional code locks a semaphore, then gets or sets the property, and then unlock the semaphore. Locking and unlocking a semaphore is expensive (although it is usually negligible) but it ensures the setting or getting process completed entirely.

nonatomic : It specifies that accessors methods simply set or return a value directly, with no guarantee about what happens if that same value is accessed simultaneously from different threads. For this reason, it’s faster to access a nonatomic property than an atomic one.

Memory Management Attributes / lifetime qualifiers :

Many languages accomplish memory management through garbage collector, but Objective-C uses a more efficient alternative called object ownership or reference counting.

strong / retain : Declaring strong means that you want to “own” the object you are referencing. The compiler will take care that any object that you assign to this property will not be destroyed as long as you or any other object points to it with a strong reference.

In Objective-C, an object is kept alive as long as it has at least one strong reference to it from another object.

In non-ARC code strong is just a synonym for retain.

A variable maintains a strong reference to an object only as long as that variable is in scope, or until it is reassigned to another object or nil.

weak : It signifies that “keep the object in memory as long as someone else points to it strongly” and you don’t want to have control over the object’s lifetime. The object you are referencing weakly only lives on because some object holds a strong reference to it once that object loses its reference this object will be destroyed.

Assign : It assigns the new value, mostly this is been used with primitive data types like int, float , NSInteger, etc.

copy : It is needed when the object is mutable. Use this if you need the value of the object as it is at the 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.

It copies the value to a different memory location and it increases the retain count, which will be released when you no longer own it but in non ARC environment it needs to be released explicitly.

Note: If you are declaring a Mutable property with copy attribute then it will actually be immutable even though you declare it mutable. When you try to append value to it, it will throw exception.

unsafe_unretained : There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. It specifies a reference that does not keep the referenced object alive and is not set to nil when there are no strong references to the object. If the object it references is deallocated, the pointer is left dangling.

getter= : To use custom name for the getter method.

setter= : To use custom name for the setter method.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s