There are two new memory management attributes for properties introduced by ARC, strong
and weak
.
Apart from copy
, which is obviously something completely different, are there any differences between strong
vs retain
and weak
vs assign
?
From my understanding, the only difference here is that weak
will assign nil
to the pointer, while assign
won't, which means the program will crash when I send a message to the pointer once it's been released. But if I use weak
, this won't ever happen, because message send to nil
won't do anything.
I don't know about any differences between strong
and retain
.
Is there any reason why should I use assign
and retain
in new projects, or are the kind of being deprecated?
Assign creates a reference from one object to another without increasing the source's retain count. Retain creates a reference from one object to another and increases the retain count of the source object.
strong is the default. An object remains “alive” as long as there is a strong pointer to it. weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
strong (default) Strong just means you have a reference to an object and you will keep that object alive. As long as you hold that reference to the object in that property, that object will not be deallocated and released back into memory.
assign -assign is the default and simply performs a variable assignment -assign is a property attribute that tells the compiler how to synthesize the property's setter implementation -I would use assign for C primitive properties and weak for weak references to Objective-C objects.
After reading so many articles Stackoverflow posts and demo applications to check variable property attributes, I decided to put all the attributes information together:
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 give best answers here!!
Variable property attributes or Modifiers in iOS
1.strong (iOS4 = retain )
Example:
@property (strong, nonatomic) ViewController *viewController; @synthesize viewController;
2.weak -
Example :
@property (weak, nonatomic) IBOutlet UIButton *myButton; @synthesize myButton;
Strong & Weak Explanation, 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).
3.retain = strong
Example:
@property (nonatomic, retain) NSString *name; @synthesize name;
4.assign
Example:
@property (nonatomic, assign) NSString *address; @synthesize address;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With