尝试使用快速枚举从父节点中删除节点时出错 [英] Error when attempting to remove node from parent using fast enumeration

查看:20
本文介绍了尝试使用快速枚举从父节点中删除节点时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After Upgrading to iOS 8 b3 and Xcode 6 b3 I get an error in the didSimulatePhysics method:

[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]非常感谢任何帮助.

Although I have exception breakpoint enabled and zombie objects I have no further info of why this is happening. The error is Thread 1 BreakPoint 1.3. [level didSimulatePhysics] Any help is much appreciated.

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

推荐答案

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

Behavior may change between iOS versions. It may have actually crashed at some point or very rarely even in Xcode 5, you just didn't get to see it.

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

The problem is easily circumvented by delaying the execution of the removeFromParent method. This should do the trick because actions are evaluated at a specific point in the game loop rather than instantaneously:

[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 并在枚举后删除该数组中的节点:

If this won't work use the "old trick": filling an NSMutableArray with to-be-deleted items and removing the nodes in that array after enumeration:

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];
}

这篇关于尝试使用快速枚举从父节点中删除节点时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆