Can't initialize C-array in Objective-C
my YYY.h file is
#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
and YYY.m is
#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}
};
...
I got error "Expected expression before { token in "boardPos[12][12] = {" string. If I write something before boardPos - it become local variable; So I cant initialize this C-array properly. I need boardPos be visible in class scope. I tried to put it in class declaration - same error.
Btw, I already rewrite it on NSArray objects but still interestin开发者_开发百科g how to deal with C-arrays.
Thx!
I think you should write this in .m file
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}
};
and remove static int boardPos[12][12]; in .h, remove boardPos[12][12] = { ... } in .m.
This will make boardPos visiable only in this .m file.
the array initialization has to be done as part of a declaration, i.e.:
in b[12] = {...};
it's not possible within a dynamic assignment...
精彩评论