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?
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.
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.
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.
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.
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)
.
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