我的 Obj-C 类中有一个 ivar,它是一个 C 数组(我对使其成为 Obj-C 属性不感兴趣)。够简单的。现在,在我的类的 init 方法中,我想使用 C 数组简写 init 为该数组播种一些值,如下面的 .m 所示。但我相当肯定这是创建一个同名的局部变量而不是初始化我的实例变量。我无法将数组 init 放入接口中,也无法在实现中声明 ivar。我只是坚持进行某种深度复制还是有其他选择?
在 GameViewController.h 中
#define kMapWidth 10
#define kMapHeight 10
@interface GameViewController : UIViewController
{
unsigned short map[kMapWidth * kMapHeight];
}
@end
在 GameViewController.m 中
- (id)init
{
if ((self = [super init]))
{
unsigned short map[kMapWidth * kMapHeight] = {
1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,
};
}
return self;
}