block定义:
typedef void(^ myBlock)(nsstring* strx);
#...
@property(nonatomic,copy)myBlock mBlock;
__weak的使用
当需要在block中引用本类的方法、变量的时候需要用__weak typeof(self) weakself = self;
目的:防止循环引用
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[weakSelf doSomething];
});
__strong的使用
当需要在block中多处引用到自身时(调用多个方法时),需要用__strong typeof(self) strongSelf = self;
目的:防止在方法中释放weak 的self,导致self = nil;
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__strong typeof(self) strongSelf = weakSelf;
[strongSelf doSomething];
[strongSelf doOtherThing];
});
__block的使用
当需要在block中用到局部变量的时候,需要用__block修饰,可以修改和使用局部变量。