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?
Best Answer
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!!
1.strong (iOS4 = retain )
Example:
2.weak -
Example :
Strong & Weak Explanation, Thanks to BJ Homer:
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:
4.assign
Example: