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

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

问题描述

升级到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];
    }
}];

虽然我启用了异常断点和僵尸对象,但我没有进一步了解为什么会发生这种情况。错误是Thread 1 BreakPoint 1.3。 [level 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天全站免登陆