将 Block 内的变量赋值给 Block 外的变量 [英] Assign a variable inside a Block to a variable outside a Block

查看:20
本文介绍了将 Block 内的变量赋值给 Block 外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个错误

变量不可赋值(缺少 __block 类型说明符)

Variable is not assignable (missing __block type specifier)

在线aPerson =参与者;.如何确保块可以访问 aPerson 变量并且可以返回 aPerson 变量?

on the line aPerson = participant;. How can I make sure the block can access the aPerson variable and the aPerson variable can be returned?

Person *aPerson = nil;

[participants enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {   
    Person *participant = (Person*)obj;

    if ([participant.gender isEqualToString:@"M"]) {
        aPerson = participant;
        *stop = YES;
    }
}];

return aPerson;

推荐答案

你需要用这行代码来解决你的问题:

You need to use this line of code to resolve your problem:

__block Person *aPerson = nil;

更多详情请参考本教程:块和变量

For more details, please refer to this tutorial: Blocks and Variables

这篇关于将 Block 内的变量赋值给 Block 外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆