-1

背景:我目前正在为 iTunes U 上斯坦福大学最新的 cs193p 课程做作业 2。我试图避免简单地查找解决方案,但我被困住了。

问题:对于以下代码:

for (Card *c in self.chosenCards) { // for debugging purposes
    NSLog(@"Card: %@ index: %d", c.contents, [self.chosenCards indexOfObject:c]);
}

[self.chosenCards removeObjectAtIndex:0];

我不断得到以下结果:

2014-11-30 14:58:11.487 Matchismo[12262:60b] Card: 5clubs♦️ index: 0
2014-11-30 14:58:11.487 Matchismo[12262:60b] Card: 6clubs♣️ index: 1
2014-11-30 14:58:23.714 Matchismo[12262:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

为什么 ): ?显然,我的数组在索引 0 处包含一个对象,并且显然数组的边界超出了 [0 .. 0]。

4

1 回答 1

0

很可能问题出在其他地方(1毕竟错误中报告的索引是)。尝试放置异常断点以识别导致异常的行(并可能为您节省大量时间并在将来遇到任何类似问题时避免混淆:))

于 2014-11-30T15:50:39.310 回答