我从未参与过非基于 ARC 的项目。我刚刚在基于 ARC 的项目中遇到了一个僵尸。我发现是因为retain cycle。我只是想知道什么是retain cycle。可以
你能给我一个保留周期的例子吗?
最佳答案
循环引用是指对象A
保留对象B
,对象B
保留对象A
的情况同时*。这是一个例子:
@class Child;
@interface Parent : NSObject {
Child *child; // Instance variables are implicitly __strong
}
@end
@interface Child : NSObject {
Parent *parent;
}
@end
您可以通过为“反向链接”使用 __weak
变量或 weak
属性来修复 ARC 中的保留周期,即链接到对象层次结构中的直接或间接父级:
@class Child;
@interface Parent : NSObject {
Child *child;
}
@end
@interface Child : NSObject {
__weak Parent *parent;
}
@end
* 这是最原始的循环保留形式;可能有一长串对象相互保留在一个圆圈中。
关于objective-c - 在 ARC 中保留循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12802396/