Can someone explain to me in detail when I must use each attribute: nonatomic, copy, strong, weak, and so on, for a declared property, and explain what each does? Some sort of example would be great also. I am using ARC.
Can someone explain to me in detail when I must use each attribute: nonatomic
Share
Nonatomic
Nonatomicwill not generate threadsafe routines thru@synthesizeaccessors.atomicwill generate threadsafe accessors soatomicvariables are threadsafe (can be accessed from multiple threads without botching of data)Copy
copyis 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
Assignis somewhat the opposite tocopy. When calling the getter of anassignproperty, 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
retainis required when the attribute is a pointer to a reference counted object that was allocated on the heap. Allocation should look something like:The setter generated by
@synthesizewill add a reference count to the object when it is copied so the underlying object is not autodestroyed if the original copy goes out of scope.You will need to release the object when you are finished with it.
@propertys usingretainwill increase the reference count and occupy memory in the autorelease pool.Strong
strongis a replacement for the retain 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 about
strongandweakfor iOS 5.http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
Weak
weakis similar tostrongexcept 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 regarding Weak and Strong.