Strong & Weak references
A weak reference is a reference that does not keep a strong hold on the instance it refers to, and so does not stop ARC from disposing of the referenced instance.
So when A has a weak reference to B, then A is NOT the owner.
Example (where A is Bone
and B is Dog
)
class Dog {
var bone: Bone?
}
class Bone {
weak var belongsTo: Dog?
}
Strong Reference
Here a Dog
can have a Bone
. In that case it's the owner of that Bone. So the bone
property is a strong reference.
Weak reference
The Bone
can belong to a Dog. But we need to declare the belongsTo
property as weak, otherwise we have a strong retain cycle (which means ARC is not going to release these object once we are done with them).
Important: In this scenario a Dog
can exists without a Bone
. And a Bone
can exists without a Dog
.
Unowned reference
Let's look at another example
class Person {
var creditCard: CreditCard?
}
class CreditCard {
unowned var owner: Person
init(owner: Person) {
self.owner = owner
}
}
Again, Person can own a CreditCard, so it has a the owner property which is a strong reference to CreditCard
.
However a CreditCard
CANNOT exist without a person. Right?
So inside CreditCard
we want a property which will always be populated but we also want it to be weak.
Something like this
weak var owner: Person
error: 'weak' variable should have optional type 'Person?'
However a weak property must be declared as Optional
so we use the unowned
which means:
I want a weak reference and it will always be populated.