ios - Objective-C:改变自身属性的递归方法

标签 ios objective-c recursion nsmutablearray enumeration

我有一个递归方法可以更改 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/

相关文章:

ios - NSArray 元素无法匹配 Swift 数组元素类型预期 NSMutableArray 但发现 __NSDictionaryI :

java - ANTLR语法中的无限递归

c++ - 对C++递归感到困惑

ios - ViewDidLoad 的随机图像生成器

ios - 在 iOS 设备中获取默认用户电子邮件

ios - DFP 问题 : No ad to show

ios - 如何实现从ControlCenter中查找?

objective-c - Cocoa:NSUserNotification出现在NSPopover后面

java - 实现自适应函数绘图

ios - 如何在向下滚动时隐藏导航栏和工具栏? Swift(如 myBridge 应用程序)