在传入block的类的dealloc方法中加断点后,dealloc方法没有被调用,确认如果在block中直接引用对象的实例变量也会引起循环引用。比较常规的解决办法就是用weak self + property的方式解决。但是个人一直觉得属性是用来向外公开的,类内部用的变量不应该用属性的方式实现。所以折腾了半天,有了以下的解决方案:
@interface TestVC (){
BOOL _isToOuter;
}
@end
@implementation TestVC
-(void)test{
NeedABlockObj* obj=[[NeedABlockObj alloc] init];
__typeof (&*self) __weak weakSelf = self;
obj.aBlock = ^(int to){
//不把weak 转为strong,则无法编译通过。
__typeof (&*self) strongSelf = weakSelf;
strongSelf->_isToOuter = YES;
};
}
@end