例如,有:
class Person {
let name: String
init(name: String) {
self.name = name
print("\(name) is being initialized")
}
deinit {
print("\(name) is being deinitialized")
}
}
//案例1:
var reference1: Person? = Person(name: "John Appleseed")
weak var reference2: Person?
weak var reference3: Person?
reference2 = reference1
reference3 = reference1
reference1 = nil
print(reference1 ?? "nil") // nil
print(reference2 ?? "nil") // Person
print(reference3 ?? "nil") // Person
//Case 2:
var reference1: Person? = Person(name: "John Appleseed")
weak var reference2: Person? = reference1
weak var reference3: Person? = reference1
reference1 = nil
print(reference1 ?? "nil") // nil
print(reference2 ?? "nil") // nil
print(reference3 ?? "nil") // nil
我想问一下,为什么弱参考在案件中有所不同?感谢帮助。