Recently, I have been working with obj-c for iOS development, and have been perplexed by the “strong” property that can be attached to variables in classes.
1) First and foremost, in a practical sense, what exactly does “strong” do?
2) I’ve noticed when constructing several obj-c classes that “strong” is typically typed in a @property context (ie @property (strong) UIImage *pic1, *pic2;) if one didn’t want to declare a variable with the property/synthesize setup, is it possible to give such a variable the “strong” attribute?
A strong reference takes ownership of an object.
When you set a strong property, the passed object is retained by the property owner, e.g.
[theViewController setString:aString];causestheViewControllerto take ownership ofaString. That object will be released when the property is set to something else.There’s a ownership qualifier,
__strong, which makes a variable behave the way I’ve described above. It is the default for any object variable —NSArray * a;is a strong reference, equivalent to__strong NSArray * a;. The one difference is that the object will be released not just when the variable is re-set, but when it goes out of scope, as at the end of a method: