Sobes.tech
Junior

What are strong memory modifiers?

sobes.tech AI

Answer from AI

The strong modifier holds a strong reference to an object, preventing its deallocation as long as at least one strong reference exists. It is used by default for variables declared without special modifiers.

Example:

class MyClass {
    var name: String = ""

    init(name: String) {
        self.name = name
        print("Object '\(name)' created")
    }

    deinit {
        print("Object '\(name)' deallocated")
    }
}

// strong reference
var obj1: MyClass? = MyClass(name: "First")
// Another strong reference
var obj2: MyClass? = obj1

print("---")

// Nullifying one reference does not lead to deallocation
obj1 = nil
print("---")

// Nullifying the last strong reference leads to deallocation
obj2 = nil
print("---")
What are strong memory modifiers? — iOS - sobes.tech