显示方法的实现Cell表明您可能来自 C++,其中构造函数以类命名。Objective-C 以不同的方式做到这一点。与您匹配的实现@interface是:
@implementation Cell
@synthesize number, checker; // implement the properties, not required in Xcode 4.4
- (id) init // the constructor
{
self = [super init]; // must invoke superclass init
if(self != nil) // check a valid object reference was returned
{
checker = ' ';
number = 1;
}
return self; // return the initialized object
}
@end
现在看起来您正在声明一个 的静态数组DrawCell,Cell *例如:
Cell *DrawCell[9];
您需要分配此数组中的单元格,循环可以做到这一点:
for(unsigned ix = 0; ix < 9; ix++)
DrawCell = [[Cell alloc] init]; // allocate & init each Cell
现在你的线:
DrawCell[3].checker = 'X';
应该可以正常工作。
有些人可能会建议您使用一个NSArray而不是 C 样式的数组,但在您的小型固定大小数组的情况下,后者很好。
其他人可能会建议您甚至不要为此烦恼一个对象,因为您似乎只存储了两个简单的数据。在这种情况下使用结构可能是一个不错的选择,例如使用:
typedef struct
{
int number;
char checker;
} Cell;
Cell DrawCell[9];
和你的线
DrawCell[3].checker = 'X';
也可以,不需要动态内存分配、属性合成等。
高温高压