Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]
来源:互联网 发布:c语言中怎样定义函数 编辑:程序博客网 时间:2024/05/30 04:53
//日期回调
**__weak typeof(self) weakSelf = self;** datePickVC.completeBlock = ^(NSString *selectDate) { [**weakSelf.**starDataBtn setTitle:selectDate forState:UIControlStateNormal]; weakSelf.starDataBtn.backgroundColor = [UIColor redColor]; };
You should review your block for:
(a) any explicit references to self
(b) any implicit references to self caused by referencing any instance variables.
Let’s imagine that we have some simple class property that was a block (this will experience the same “retain cycle” warnings as your question, but will keep my examples a little simpler):
@property (nonatomic, copy) void (^block)(void);
And let’s assume we had some other class property we wanted to use inside our block:
@property (nonatomic, strong) NSString *someString;
If you reference self within the block (in my example below, in process of accessing this property), you will obviously receive that warning about the retain cycle risk:
self.block = ^{
NSLog(@”%@”, self.someString);
};
That is remedied via the pattern you suggested, namely:
__weak typeof(self) weakSelf = self;
self.block = ^{
NSLog(@”%@”, weakSelf.someString);
};
Less obvious, you will also receive the “retain cycle” warning if you reference an instance variable of the class inside the block, for example:
self.block = ^{
NSLog(@”%@”, _someString);
};
This is because the _someString instance variable carries an implicit reference to self, and is actually equivalent to:
self.block = ^{
NSLog(@”%@”, self->_someString);
};
One might be inclined to just use weakSelf pattern again, resulting in the weakSelf->_someString syntax, but the compiler will warn you about this:
Dereferencing a __weak pointer is not allowed due to possible null value caused by race condition, assign it to strong variable first
You therefore resolve this by using the weakSelf pattern, but also create a local strong variable within the block and use that to dereference the instance variable:
__weak typeof(self) weakSelf = self;
self.block = ^{
__strong typeof(self) strongSelf = weakSelf;
if (strongSelf) { NSLog(@"%@", strongSelf->_someString); // or better, just use the property // // NSLog(@"%@", strongSelf.someString);}
};
- Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]
- Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]
- 防止出现 Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]
- Capturing 'self' strongly in this block is likely to lead to a retain cycle
- capturing self strongly in this block is likely to lead to a retain cycle
- Capturing self strongly in this block is likely to lead to a retain cycle
- Capturing 'self' strongly in this block is likely to lead to a retain cycle
- Capturing 'self' strongly in this block is likely to lead to a retain cycle
- Capturing 'self' strongly in this block is likely to lead to a retain cycle
- capturing self strongly in this block is likely to lead to a retain cycle
- Capturing 'self' strongly in this block is likely to lead to a retain cycle
- Capturing 'self' strongly in this block is likely to lead to retain cycle 解决方案
- Capturing self strongly in this block is likely to lead to retain cycle解决方案
- Capturing self strongly in this block is likely to lead to retain cycle
- Capturing self strongly in this block is likely to lead to retain cycle的问题
- iOS之Block报错:capturing self strongly in this block is likely to lead to a retain cycle
- iOS之Block报错:capturing self strongly in this block is likely to lead to a retain cycle
- iOS--错误集锦--Capturing 'self' strongly in this block is likely to lead to a retain cycle
- 数据库三范式
- 分布式缓存技术redis学习系列----深入理解Spring Redis的使用
- PostgreSQL数据类型
- Meshlab读取三维点云、三维点云重建以及三维点云法向量计算
- Java回调机制
- Capturing 'self' strongly in this block is likely to lead to a retain cycle [duplicate]
- 函数返回值
- 未清采购订单关闭最全的方法
- storm 通用模板
- LBaas之(四)Neutron/LBaaS/API
- 使用ESP8266WIFI模块心得
- 深度前馈网络【DeepLearningBook读书笔记】
- 【spring】事务
- 28.输入两个链表,找出它们的第一个公共结点。