下面的代码CreditCard类具有称为person的弱属性,如果我尝试将nil分配给person并将信用卡对象分配为nil,则不会从内存中释放对象,但是如果我在CreditCard类中使用无人属性则代替了weak,成功解除分配,为什么弱在这里不能工作?
class Person{
var card:CreditCard?
deinit {
print("Person Deallocated")
}
}
class CreditCard{
weak var person:Person?
init(enterowner:Person) {
self.person = enterowner
}
deinit {
print("credit card deaalocated.")
}
}
var peroson:Person? = Person()
var mycard:CreditCard? = CreditCard(enterowner: peroson!)
peroson?.card = mycard
peroson = nil
mycard = nil