关于ARC,下面的代码中Kraken是否强引用了Sucker?

In regards to ARC, in the following code does Kraken have a strong reference to Sucker?

我正在阅读 Swift 中有关 ARC 的内容,以便更好地了解内存分配和释放。在我正在阅读的以下博客中,我了解到 Kraken 对 Tentacle 有很强的参考,然后 Tentacle 对 Sucker 有很强的参考,但是我很想知道 Kraken 是否与 Sucker 有很强的关系?

代码如下:

class Kraken {
    let tentacle = Tentacle() //strong reference to child.
}
class Tentacle {
    let sucker = Sucker() //strong reference to child
}
class Sucker {}

不,没有。 sucker 对象的引用计数仍然是 1Kraken 切断他的 tentacle 的那一刻,sucker 也会消失 ;)

换句话说,间接强引用不会增加 ARC 内存管理器使用的引用计数器。

快速测试:

let kraken = Kraken()
let refCount = CFGetRetainCount(kraken.tentacle.sucker)
print("ref count: \(refCount)")

输出:

ref count: 2

你应该读作 1,因为它包括 CFGetRetainCount 函数 argument 保留的引用。