Variable 'bop' is uninitialized when captured by block
代码:
- (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]; }