背景:我目前正在为 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]。