Trying to understand the strong vs weak reference in Objectice-C by reading this explanation, it makes prefect sense to me. But there is one scenario that I cannot really figure it out.
Suppose it's in ARC environment, and I will use -(hyphen) as strong reference and .(dot) as weak reference for now. Let's say I have a View Controller object MyViewController vc = [MyViewController alloc] init];
and it has view, so their relation is like
vc ------ view
with strong reference. Once vc
is deallocated, view
will also be deallocated.
If I want to add subviews to the view, for example, UILabel, from Interface Builder and connect it to the object, usually I will have declare a weak reference ivar @property (weak, nonatomic) IBOutlet UILabel *myLabel
because the view already have a strong reference to it. So now the relationship looks like
vc ------ view ------ myLabel
and
vc .................. myLabel
So when vc
is deallocated, view
gets deallocated, and then myLabel
also gets deallocated. But what if I also set a strong reference between vc
and mylabel
, the relationships between now become
vc ------ view ------ myLabel
and
vc ------ myLabel
When vc
is deallocated, will myLabel also get deallocated? I think so because no objects have strong reference to it now. But I want to make sure. Please let me know if I am missing anything here. Thanks in advance.