Values & References14 / 16
Up next

ARC and deinit

class Resource {
    let name: String
    init(_ name: String) { self.name = name }
    // deinit runs when the last reference goes away
    deinit { print("freeing \(name)") }
}
var a: Resource? = Resource("db")
// dropping the only reference triggers deinit now
a = nil
print("done")
What you're solving

the last reference frees the object