Objective-C ++块与Objective-C块 [英] Objective-C++ block vs Objective-C block

查看:100
本文介绍了Objective-C ++块与Objective-C块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我具有有效的代码:

In Objective-C I have the valid code:

TestTwo.h:

TestTwo.h:

@interface TestTwo : NSObject
-(void)test;
@end

TestTwo.m:

TestTwo.m:

@implementation TestTwo
-(void)test
{
    void (^d_block)(void) = 
    ^{
        int n;
    };
}
@end

我真正想要的是一个Objective-C ++类,该类定义类似于test的方法.这是简化的操作,但可以说明其意图.因此,在Objective-C ++中,我有:

What I really want is an Objective-C++ class that defines a method similar to test. This is simplification, but illustrates the intent. So, in Objective-C++ I have:

Test.h:

class Test
{
public:
    void TestIt();
};

Test.mm:

#include "Test.h"

void Test::TestIt()
{
    void (^d_block)(void) = 
    ^{
        int n;
    };
}

我收到以下错误:

错误:"int Test :: n"不是类Test"的静态成员.

error: 'int Test::n' is not a static member of 'class Test'.

如果删除int n;,则没有错误.在这种情况下,如何在块内定义n?

If I remove int n; there is no error. How do I define n within the block in this context?

推荐答案

这是

This is a GCC bug filed under radar #8953986. You can either use Clang/LLVM 2.0+ to compile your code as is, or put your block variables in the global name space (i.e., int ::n) and use GCC. Note that using the global name space in this case is not valid C++ and Clang/LLVM 2.0+ won’t compile it.

这篇关于Objective-C ++块与Objective-C块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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