码迷,mamicode.com
首页 > 其他好文 > 详细

Variable 'bop' is uninitialized when captured by block

时间:2015-05-02 18:10:13      阅读:2866      评论:0      收藏:0      [点我收藏+]

标签:

代码:

- (void)doTest {
    NSBlockOperation * bop = [NSBlockOperation blockOperationWithBlock:^{
        if (!bop.isCancelled) {
            // ...
            // doSomething
        }
        
    }];
    
    [mQueue addOperation:bop];
}

 

这段代码是有明显问题的,如果用Analyze来检查的话,就会有警告Variable ‘bop‘ is uninitialized when captured by block.

其实很简单,问题出在block内部对于bop的使用上. 在进入block的时候,bop是一个未初始化的变量.

解决方法很简单,加上__block修饰为block变量就好了.

- (void)doTest {
    __block NSBlockOperation * bop = [NSBlockOperation blockOperationWithBlock:^{
        if (!bop.isCancelled) {
            // ...
            // doSomething
        }
        
    }];
    
    [mQueue addOperation:bop];
}

 

Variable 'bop' is uninitialized when captured by block

标签:

原文地址:http://www.cnblogs.com/agger0207/p/4472241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!