-1

我的项目是制作 Checker 土耳其语游戏

我做了2个班板和单元

在 Cell.h 中

#import <Foundation/Foundation.h>

@interface Cell : NSObject

{
    int number;
    char checker;
}
@property (nonatomic ) int number;
@property (nonatomic ) char checker;
@end

在 Cell.m 中

#import "Cell.h"

@implementation Cell
-(void)Cell{
    checker=' ';
    number=1;
}
@end

但是在board.m中我尝试了很多方法但是这里没有成功是我打印出检查器的代码

DrawCell[ 3 ].checker  = 'X';

结果是旋转的问号,它们的数字都是0,我正在尝试更改它们,但它们是0

谢谢

4

1 回答 1

1

显示方法的实现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

现在看起来您正在声明一个 的静态数组DrawCellCell *例如:

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';

也可以,不需要动态内存分配、属性合成等。

高温高压

于 2012-09-09T02:38:48.403 回答