WTH is Strong, Weak, Unowned in Swift?

January 19, 2019

A strong reference is whenever we declare a variable without specifying its reference type, it will always be strong. A reference being strong means that the ARC will increment the reference count for the object being referenced by a variable. This impacts memory management since an object’s memory can not be released while the reference count is greater than zero.

A weak reference allows the possibility of it to become nil (this happens automatically when the referenced object is deallocated), therefore the type of your property must be optional - so you, as a programmer, are obligated to check it before you use it (basically the compiler forces you, as much as it can, to write safe code).

An unowned reference presumes that it will never become nil during it's lifetime. A unowned reference must be set during initialization - this means that the reference will be defined as a non-optional type that can be used safely without checks. If somehow the object being referred is deallocated, then the app will crash when the unowned reference will be used.