【发布时间】:2013-06-05 21:47:49
【问题描述】:
我有一个递归方法可以改变 self 的属性(定义方法的对象)。
我收到错误:
...未捕获的异常 'NSGenericException',原因:'*** Collection <__nsarraym:> 在枚举时发生了变异...'
我阅读了有关此错误的信息,但我不确定如何对我的问题应用解决方案。递归对于解决方案至关重要,更新此特定属性也是如此。我对 Objective-C 很陌生,所以我可能遗漏了一些东西或者设计这个解决方案很糟糕。
-
currentPlayer是财产 -
recursiveMethod显然是产生错误的递归方法
从for in 循环中调用此递归方法。
- (void) recursiveMethod:(id <Team>)team atIndex:(int *)i withPlayer:(id <Player>) {
[self.currentPlayer replaceObjectAtIndex:i withObject:nextPlayer];
if // some conditional that's unimportant to this question
{
// grab another team
// grab another index
// grab another player
[self recursiveMethod:nextTeam atIndex:i withPlayer:nextPlayer];
}
}
很多细节并不重要。我把它剥掉了;实际上它只是一个递归方法,它将更新定义该方法的对象的属性(在本例中为数组)。
【问题讨论】:
-
哇。看看我在谷歌上搜索到的确切的错误消息:stackoverflow.com/questions/10157381/…
-
GuillaumeA 指出的答案说您可以通过自己迭代来避免错误。但是,在代码示例中,您展示的正是您正在做的事情 - 对数组的递归修改。所以你必须有一个枚举(例如
for in),你在其中调用recursiveMethod。
标签: ios objective-c recursion nsmutablearray enumeration