【iOS】self与block的使用规范

避免block中循环引用,内存释放不了的问题,规范编码很重要。

相对来说,在block中操作self时,正确的使用姿势。

 1 BlockManager *bm = [BlockManager sharedInstance];
 2     
 3 __weak typeof(self) weakSelf = self;
 4 [bm doSomethingWithBlock:^NSString *(NSInteger value) {
 5     __strong typeof(weakSelf) strongSelf = weakSelf;   //不能缺少
 6     if (strongSelf) {
 7         NSLog(@"%@", strongSelf.array[7]);
 8         [strongSelf printMsg];
 9         return @"YES";
10     }
11     
12     return @"NO";
13 }];

 

posted @ 2016-04-05 17:58  Mr轨迹  阅读(373)  评论(0编辑  收藏  举报