我有一个递归方法可以更改 self(定义该方法的对象)的属性。
我收到错误:
...uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x755f670> was mutated while being enumerated...'
我读到了这个错误,但我不确定如何应用解决方案来解决我的问题。递归对于解决方案至关重要,更新此特定属性也是如此。我是 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];
}
}
很多细节并不重要。我把它剥下来了;实际上,它只是一个递归方法,它将更新定义该方法的对象的属性(在本例中为数组)。
最佳答案
只需阅读异常消息:
NSArrayM [...] was mutated while being enumerated.
你不能那样做。
关于ios - Objective-C:改变自身属性的递归方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16950474/