开始NSMutableArray - 忽略调试

时间:2013-05-09 13:17:19

标签: objective-c cocoa nsmutablearray

我只是在学习目标-c。希望这对于之前使用过NSMutableArrays的人来说是一个简单的修复。

注意:我在你看过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]];
}

如果您发现其他被视为“错误代码”的内容,我也很乐意接受这些反馈。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

而不是[selections initWithObjects: @"", @"", @"", @"", @"", @"", @"", @"", @"", nil];

selections = [[NSMutableArray alloc] initWithObjects: @"", @"", @"", @"", @"", @"", @"", @"", @"", nil];

答案 1 :(得分:-1)

尝试将[NSNull null]添加到该数组。您可以在数组中添加空对象而不是空字符串

最好的问候