block的循环引用问题(self ->block ->self)
定义一个block
typedef void(^MyBlock)(BlockViewController *);
typedef void(^MyBlock)();
@property (nonatomic,copy) MyBlock mblock;
1)__block
- (void)block01 {
__block BlockViewController *blockVC = self;
self.mblock = ^(){
NSLog(@"mblock---%@",blockVC.name);
blockVC = nil;
};
self.mblock();
}
2)__weak
- (void)block01 {
__weak typeof(self) weakself =self;
self.mblock = ^(){
NSLog(@"mblock---%@", weakself.name);
};
self.mblock();
}
3)__strong
- (void)block01 {
__weak typeof(self) weakself =self;
self.mblock = ^(){
__strong typeof(self) strongself = weakself;
NSLog(@"mblock---%@", strongself.name);
};
self.mblock();
}
4)@weakify(self) @strongify(self)
YYKit和RAC
- (void)block01 {
@weakify(self)
self.mblock = ^(){
@strongify(self)
NSLog(@"mblock---%@", self.name);
};
self.mblock();
}
5)传参数
- (void)block01 {
self.mblock = ^(BlockViewController *vc){
NSLog(@"mblock---%@", vc.name);
};
self.mblock(self);
}
iOS容易造成循环引用的三种场景
1.NSTimer
2.block
3.delegate