一个用Block实例变量语法,当有一个参考的实例变量,常引起retain cycle。

capturing self strongly in this block is likely to lead to a retain cycle

_items = [[NSMutableArray alloc] init];
_block = ^{
[_items addObject:@"Hello!"]; //_block引用了_items,导致retain cycle。
};

写成以下格式

__block ViewController *blockSelf = self;
_block = ^{
[blockSelf->_items addObject:@"Hello!"];
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

04-27 17:56