将块内的变量分配给块外的变量 [英] Assign a variable inside a Block to a variable outside a Block

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

问题描述

我收到错误


变量不可指派(缺少__block类型说明符)

Variable is not assignable (missing __block type specifier)

aPerson = participant; 。我如何确保块可以访问 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

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

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