Here is what I know about variable properties
- atomic //default
- nonatomic
- strong=retain //default
- weak
- retain
- assign //default
- unsafe_unretained
- copy
- readonly
- readwrite //default
so below is the detailed article link where you can find above mentioned all attributes, that will definitely help you.
Many thanks to all the people who have given the best answers here!!
Variable property attributes or Modifiers in iOS
01.strong (iOS4 = retain )
- it says "keep this in the heap until I don't point to it anymore"
- in other words " I'm the owner, you cannot dealloc this before aim fine with that same as retain"
- You use strong only if you need to retain the object.
- By default, all instance variables and local variables are strong pointers.
- We generally use strong for UIViewControllers (UI item's parents)
- strong is used with ARC and it basically helps you, by not having to worry about the retain count of an object. ARC automatically releases it for you when you are done with it. Using the keyword strong means that you own the object.
Example:
@property (strong, nonatomic) ViewController *viewController;
@synthesize viewController;
02.weak (iOS4 = unsafe_unretained )
- it says "keep this as long as someone else points to it strongly"
- the same thing as assign, no retain or release
- A "weak" reference is a reference that you do not retain.
- We generally use weak for IBOutlets (UIViewController's Childs).This works because the child object only
needs to exist as long as the parent object does.
- a weak reference is a reference that does not protect the referenced object from collection by a garbage collector.
- Weak is essentially assign, a unretained property. Except the when the object is deallocated the weak pointer is automatically set to nil
Example :
@property (weak, nonatomic) IBOutlet UIButton *myButton;
@synthesize myButton;
Explain:Thanks to BJ Homer
Imagine our object is a dog, and that the dog wants to run away (be deallocated).
Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog will not run away. If five people attach their leash to one dog, (five strong pointers to one object), then the dog will not run away until all five leashes are detached.
Weak pointers, on the other hand, are like little kids pointing at the dog and saying "Look! A dog!" As long as the dog is still on the leash, the little kids can still see the dog, and they'll still point to it. As soon as all the leashes are detached, though, the dog runs away no matter how many little kids are pointing to it.
As soon as the last strong pointer (leash) no longer points to an object, the object will be deallocated, and all weak pointers will be zeroed out.
When we use weak?
The only time you would want to use weak, is if you wanted to avoid retain cycles
(e.g. the parent retains the child and the child retains the parent so neither is ever released).
Best Answer
Nonatomic
Nonatomic
will not generate threadsafe routines thru@synthesize
accessors.atomic
will generate threadsafe accessors soatomic
variables are threadsafe (can be accessed from multiple threads without botching of data)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 a reference counted object that was allocated on the heap. Allocation should look something like:The setter generated by
@synthesize
will 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.
@property
s usingretain
will increase the reference count and occupy memory in the autorelease pool.Strong
strong
is 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
strong
andweak
for iOS 5. http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1Weak
weak
is similar tostrong
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 regarding Weak and Strong.