我只是在学习objective-c。希望这是一个简单的修复方法,适用于以前曾使用过NSMutableArrays的任何人。开始NSMutableArray - 调试忽略
注意:我在看过3个视频的同时阅读了5个文章,然后再问这个问题。有足够的资源可用于setValue:forKey,但我不相信这适用于此场景。
我想用一个简单的数组来支持在tic tac toe游戏中做出的选择。这个概念很简单: - 阵列实例化为9个阵列节点 - 当进行选择时,当前字母将被放置在相应的阵列节点中 - 检查游戏结束的方法将引用阵列节点。
问题: - 我试图为每个默认对象设置NSNull
值而不是@""
。我收到一个错误"Unexpected interface name 'NSNull'
。 - 调试阵列节点的NSLog逻辑从不执行。我的假设是阵列没有被填充。
@synthesize lblStatus, currLetter;
@synthesize selections;
@synthesize btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
- (void)viewDidLoad
{
[super viewDidLoad];
[selections initWithObjects:
@"", @"", @"", @"", @"", @"", @"", @"", @"", nil];
currLetter = @"X";
[self updateTitle];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)takeTurn:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.currentTitle == nil) {
[selections replaceObjectAtIndex:btn.tag withObject:currLetter];
[sender setTitle:currLetter forState:UIControlStateNormal];
[self changeTurns];
}
}
- (void)changeTurns {
if ([currLetter isEqualToString:@"X"]) {
currLetter = @"O";
} else {
currLetter = @"X";
}
for (NSString *node in selections) {
NSLog([NSString stringWithFormat:@"Node: %@", node]);
}
[self updateTitle];
}
- (void)updateTitle {
[lblStatus setText:[NSString stringWithFormat:@"%@'s Turn", currLetter]];
}
如果你看到别的被认为是“不好的代码”,我会很乐意采取这种反馈也。
任何帮助,非常感谢。
决不叫'-init'方法,无需先调用'+ alloc',像这样:'[[美孚ALLOC] INIT]'。 – Caleb 2013-05-09 13:48:22