无法在Objective-C中初始化C数组

时间:2010-01-17 06:58:58

标签: objective-c

我的YYY.h文件是


 #define W 1 // i am
 #define B 2 // opponent
 #define F 3 // board margin
static int boardPos[12][12];
@interface YYY : NSObject 
{...}
-(id)init;
@end

和YYY.m是


#import "YYY.h"


@implementation YYY

-(id)init
{   
    if (self = [super init]) {

        // initializing Empty Board

        boardPos[12][12] = {
            {F,F,F,F,F,F,F,F,F,F,F,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,0,0,0,0,0,0,0,0,0,0,F},
            {F,F,F,F,F,F,F,F,F,F,F,F}
        };
...

我收到错误“在{token in”boardPos [12] [12] = {“string之前的预期表达式。如果我在boardPos之前写了一些内容 - 它变成了局部变量;所以我无法正确初始化这个C数组。我需要boardPos在类范围内可见。我试着把它放在类声明中 - 同样的错误。

顺便说一句,我已经在NSArray对象上重写了它,但仍然很有趣如何处理C数组。

THX!

2 个答案:

答案 0 :(得分:5)

我认为你应该在.m文件中写这个

static int boardPos[12][12] = {
        {F,F,F,F,F,F,F,F,F,F,F,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,0,0,0,0,0,0,0,0,0,0,F},
        {F,F,F,F,F,F,F,F,F,F,F,F}
    };

并删除static int boardPos [12] [12];在.h中,删除boardPos [12] [12] = { ......} .m。

这将使boardPos仅在此.m文件中可见。

答案 1 :(得分:3)

数组初始化必须作为声明的一部分完成,即:

in b[12] = {...};

在动态作业中不可能......