萌新在iOS开发时,使用block时候经常会遇到这个错误
Variable is not assignable (missing __block type specifier)
例如这样子的写法,block
内部引用了一个外部对象
Person *aPerson = nil;
[participants ObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Person *participant = (Person*)obj;
if ([participant.gender isEqualToString:@"M"]) {
aPerson = participant;
*stop = YES;
}
}];
解决办法也很简单,就是在block
外部这样声明就可以了。
__block Person *aPerson = nil;
即如下的样子
__block Person *aPerson = nil;
Person *aPerson = nil;
[participants ObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Person *participant = (Person*)obj;
if ([participant.gender isEqualToString:@"M"]) {
aPerson = participant;
*stop = YES;
}
}];