Differences between strong and weak in objective-c
A strong reference (which you will use in most
cases) means that you want to "own" the object you are referencing with
this property/variable. The compiler will take care that any object
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. Only once
you set the property to
In contrast, with a weak reference you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to
nil
will the object get destroyed (unless one or more other objects also hold a strong reference to it).In contrast, with a weak reference you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to
nil
. The most frequent use cases of weak references in iOS are:- delegate properties, which are often referenced weakly to avoid retain cycles, and
- subviews/controls of a view controller's main view because those views are already strongly held by the main view.
Comments
Post a Comment