weak 可避免死循环。
代码例子:
@class ClassA;
@interface WBViewController : UIViewController
@property(nonatomic, strong) ClassA *classA;
@interface ClassA : NSObject
//为避免死循环,这里应该改为 weak
@property(nonatomic, strong) WBViewController *controller;
---------------------------------
@implementation WBViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ClassA *aClass = [[ClassA alloc] init];
aClass.controller = self;
// 出错!self.classA 为strong, aClass.controller 也是strong,
// 以下赋值会引起引用传说中的死循环(retain cycle)
// 当系统要释放 aClass 时,发现 aClass 有strong reference (controller), 得先释放 controller,
// 轮到释放 controller 时,又发现 controller 也有 strong reference (classA), 于是又去释放的 classA...
self.classA = aClass;
}
本文介绍如何通过使用weak属性来避免iOS应用中WBViewController与ClassA之间的强引用循环问题,从而防止内存泄漏。

442

被折叠的 条评论
为什么被折叠?



