为什么当 ActiveReferenceCount > 0 时对象被取消初始化

Why object is being deinitialized when ActiveReferenceCount > 0

我正在阅读有关 ARC 的文章,并对增加 Reference Count 感到困惑。我的示例代码如下。
我的第一个 class:

class Owner{
var name: String
weak var cat:Cat? //Cat reference count will not increase because of 'weak'
init(name: String){
    self.name = name
    print("Owner class is initialized.")
  }

deinit{
    print("Owner class is deinitialized.")
  }  
}

我的第二个 Class:

class Cat{
var name: String
var owner: Owner?

init(name: String){
    self.name = name
    print("Cat class is initialized.")
  }

deinit {
    print("Cat class is deinitilized.")
  }
}

我的Class使用代码:

var mamun: Owner? = Owner(name: "Mamun") //OwnerAR = 1
var vutu: Cat? = Cat(name: "Vutu") // CatAR = 1
mamun?.cat = vutu // CatAr = 1
vutu?.owner = mamun // OwnerAR = 2
mamun = nil // OwnerAR = 1
vutu = nil // CatAR = 0

输出:

Owner class is initialized.
Cat class is initialized.
Cat class is deinitilized.
Owner class is deinitialized.

困惑:当Owner引用计数为1时,为什么在vutu = nil代码执行后它被取消初始化。我在计算参考文献时做错了吗?

当您将 vutu 设置为 nil 时,该对象的引用计数变为零并按预期取消初始化。在取消初始化的过程中,它会释放对其拥有的对象的任何强引用。所以它的 owner 被释放了。这将使 mamun 变量引用的对象的引用计数为 0。这就是为什么您会看到 Owner class is deinitialized. 的最后一条消息。

mamun?.cat = 看到 // CatAr = 1 (1 by vutu)

vutu?.owner = mamun // OwnerAR = 2 (1 by vutu's owner and another by mamun)

mamun = nil // OwnerAR = 1 (1 by vutu's owner) == cannot deinit until vutu deinit

已查看 = 无 // CatAR = 0 == vutu deinit == OwnerAR = 0 == mamun deinit