【问题标题】:Error when attempting to remove node from parent using fast enumeration尝试使用快速枚举从父节点中删除节点时出错
【发布时间】:2014-07-08 07:53:42
【问题描述】:

升级到 iOS 8 b3 和 Xcode 6 b3 后,didSimulatePhysics 方法出现错误:

[self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode *node, BOOL *stop) {
    if (node.position.y < 0 || node.position.x>320 || node.position.x<0) {
        [node removeFromParent];
    }
}];

虽然我启用了异常断点和僵尸对象,但我没有进一步的信息说明为什么会发生这种情况。错误是线程 1 断点 1.3。 [级别 didSimulatePhysics] 非常感谢任何帮助。

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x7edf17d0> was mutated while being enumerated.'

【问题讨论】:

  • 信息不够;如果你不能提供更多,那么这里的任何人都无能为力(恕我直言)。
  • @trojanfoe 如何获取有关该错误的更多信息?
  • @trojanfoe 编辑了更多信息
  • OK 查找迭代 NSMutableArray 并尝试在循环内更改它的代码。这就是异常的原因。
  • 我不确定;当然有可能,但更可能是 iOS 中的错误,而不是 Xcode。

标签: ios iphone objective-c xcode sprite-kit


【解决方案1】:

iOS 版本之间的行为可能会有所不同。即使在 Xcode 5 中,它实际上也可能在某个时候崩溃,或者很少发生,你只是没有看到它。

通过延迟 removeFromParent 方法的执行很容易解决这个问题。这应该可以解决问题,因为动作是在游戏循环中的特定点而不是瞬间进行评估的:

[self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode *node, BOOL *stop) {
    if (node.position.y < 0 || node.position.x>320 || node.position.x<0) {
        [node runAction:[SKAction removeFromParent]];
    }
}];

如果这不起作用,请使用“旧技巧”:用要删除的项目填充 NSMutableArray 并在枚举后删除该数组中的节点:

NSMutableArray* toBeDeleted = [NSMutableArray array];

[self enumerateChildNodesWithName:@"name" usingBlock:^(SKNode *node, BOOL *stop) {
    if (node.position.y < 0 || node.position.x>320 || node.position.x<0) {
        [toBeDeleted addObject:node];
    }
}];

for (CCNode* node in toBeDeleted)
{
    [node removeFromParent];
}

【讨论】:

  • 感谢您的回复,但不幸的是它并没有以这种方式工作。所以解决方法是用 [self addChild..] 之后的节点填充 NSMutableArray 并在 [self enumerate..] 中的循环中将其删除?这会不会导致更多的性能问题(在 enumerate-block 中运行循环)?
  • 已编辑答案以及删除数组的代码示例...这不应该是性能问题,还是您希望应用程序崩溃或运行效率稍低? ;)
  • 没有任何变化。同样的错误。我认为这是 b3 中的错误,因为我在 b2 和 b1 中都没有错误。其他人可以检查吗?
  • 使用第二个代码示例,崩溃究竟发生在哪里?尝试添加异常断点。
  • 您是否正在运行添加或删除子节点的后台线程?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多