2013-05-09 78 views
0

我只是在学习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]]; 
} 

如果你看到别的被认为是“不好的代码”,我会很乐意采取这种反馈也。

任何帮助,非常感谢。

+1

决不叫'-init'方法,无需先调用'+ alloc',像这样:'[[美孚ALLOC] INIT]'。 – Caleb 2013-05-09 13:48:22

回答

1

代替[selections initWithObjects: @"", @"", @"", @"", @"", @"", @"", @"", @"", nil];

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

+0

工作完美!非常感谢您的快速响应。 – alockrem 2013-05-09 13:37:44

+0

不客气,好友 – 2013-05-09 13:38:21

-1

尝试增加[NSNull空]到该阵列。您可以在阵列内添加空对象,而不是空字符串的

问候

+1

你预计这会产生什么不同,为什么? – Caleb 2013-05-09 13:49:23

+0

点击相同的按钮将保持转变。还需要添加一个验证。如果在用户点击按钮时在数组内添加空对象,请检查该对象是否为类[NSNull类]。如果不是,请不要改变转弯并让用户选择或继续您的实施 – Arun 2013-05-10 05:35:29

+1

我很欣赏反馈,并且我同意NSNull是我最初想要使用的方法。尽管如此,该软件并没有如你所描述的那样反应。一旦标题已应用(X或O),轻击同一个按钮将被忽略。 – alockrem 2013-05-10 14:41:12