Sobes.tech
Junior — Middle

Kas objekt eemaldatakse automaatselt mälust, kui sellele viitab kolm nõrka (weak) ja üks mittekohustuslik (unowned) viide?

sobes.tech AI

Vastus AI-lt

Object will be automatically removed from memory if there are no strong references left to it. Weak (weak) and unowned (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