Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strong vs Retain in ARC

I know there have been numerous write ups on 'strong' vs. 'weak'. But all docs says that both are 100% synonyms for each other and you can use 'strong' in replace of 'retain', and vice versa.

My question is: if they are same, why did Apple introduce the new 'strong' keyword? I have tested both in a sample project, and both the 'strong' and 'retain' property attributes appear to do the same thing. Don't you think that if Apple introduced the 'strong' attribute, it should disallow the use of 'retain' attribute? Or am I missing something?

like image 777
Anshul Avatar asked Mar 03 '15 11:03

Anshul


People also ask

What is difference between assign and retain?

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.

What is strong and Nonatomic in Objective C?

nonatomic property means @synthesize d methods are not going to be generated threadsafe -- but this is much faster than the atomic property since extra checks are eliminated. strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object.

What is strong attribute of property?

strong / retain : Declaring strong means that you want to “own” the object you are referencing. Any data 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.

What is strong and weak reference in Swift?

For example, a strong reference keeps a firm hold on instances and doesn't allow deallocation by ARC. Similarly, a weak reference cannot protect the instances from being deallocated by ARC. Before you learn about strong and weak reference, make sure to understand how classes and objects work in Swift.


1 Answers

retain is a leftover from the pre-ARC days where you would increase/decrease an objects retain count depending on whether you wanted it to hang around in memory.

Obviously with ARC you no longer have to worry about this and I suspect that retain may simply have been left in for ease of use for the more veteran objective-c programmers out there.

The keywords that are most prevalent with arc are: (strong, weak, nonatomic, readonly, copy).

like image 115
Rob Sanders Avatar answered Sep 17 '22 22:09

Rob Sanders