Sobes.tech
Junior — Middle

Will the object be automatically removed from memory if it is referenced by three weak and one unowned reference?

sobes.tech AI

Answer from AI

An object will be automatically removed from memory if there are no strong references to it. Weak and unowned references do not increase the reference count of the object. In your case, if there are only three weak and one unowned references, but no strong references, the object is considered unused and will be deallocated.

Example:

class MyClass {}

var strongRef: MyClass? = MyClass() // strong reference
weak var weakRef1 = strongRef
weak var weakRef2 = strongRef
weak var weakRef3 = strongRef
unowned var unownedRef = strongRef!

strongRef = nil // now there are no strong references, the object will be deleted
Will the object be automatically removed from memory… - sobes.tech