美文网首页iOS
Variable is not assignable (miss

Variable is not assignable (miss

作者: MccReeee | 来源:发表于2017-07-21 12:37 被阅读1110次
image.pngimage.png

萌新在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;
    }
}];

相关文章

网友评论

    本文标题:Variable is not assignable (miss

    本文链接:https://www.haomeiwen.com/subject/boaskxtx.html