gpt4 book ai didi

ios - NSMutableArray 索引变得非常高并且 removeObjectAtIndex 超出范围

转载 作者:行者123 更新时间:2023-11-28 19:07:24 25 4
gpt4 key购买 nike

我正在 SpriteKit 中进行测试和原型(prototype)制作,并拥有双击翻转的 Sprite 。我在 _theArray 中有 52 个 sprite,它包含当前在 View 中的 sprite 的名称。当我选择一个 sprite 时,我希望它在 zPosition 线程中位于顶部,通过实现这一点,我使用以下代码刷新所有 sprite 的 zPositions:

- (void)setTheZposition:(NSString *)nodeNameToCheck {

NSLog(@"++setTheZposition:++");

if (![_node.name isEqual: @"background"]) { // Do not react if background
[self enumerateChildNodesWithName:nodeNameToCheck usingBlock:^(SKNode *node, BOOL *stop) {

NSUInteger index = [_theArray indexOfObjectPassingTest:
^(id obj, NSUInteger idx, BOOL *stop) {
return [obj hasPrefix:nodeNameToCheck];
}];
NSLog(@"index: %i & length _theArray: %i", index, [_theArray count]);
[_theArray removeObjectAtIndex:index];
[_theArray insertObject:nodeNameToCheck atIndex:0];


// Refresh the zPosition for all sprites
float positionZ = (int) [_theArray count];
for (NSString *theNode in _theArray) {
SKNode *refreshNode = [self childNodeWithName:theNode];
refreshNode.zPosition = positionZ;
positionZ--;
}
}];
}
}

只要我只拖动 Sprite ,代码就可以找到,但是当我替换 Sprite 并删除原始 Sprite 时,我删除了 index = 0 并添加了 index = 0_theArray 中,这似乎是导致问题的原因。

崩溃发生在以下行:

[_theArray removeObjectAtIndex:index];

...索引为:2147483647

...而它应该是 0

崩溃日志:

2013-09-29 21:08:59.445 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:08:59.446 testButtonsetc[11597:a0b] index: 51 & length _theArray: 52
2013-09-29 21:09:04.112 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:09:04.112 testButtonsetc[11597:a0b] index: 0 & length _theArray: 52
2013-09-29 21:09:04.343 testButtonsetc[11597:a0b] >>>>>>>>T-A-P<<<<<<<<<
2013-09-29 21:09:07.277 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:09:07.278 testButtonsetc[11597:a0b] index: 0 & length _theArray: 52
2013-09-29 21:09:07.477 testButtonsetc[11597:a0b] >>>>>>>>T-A-P<<<<<<<<<
2013-09-29 21:09:10.851 testButtonsetc[11597:a0b] ++setTheZposition:++
2013-09-29 21:09:10.851 testButtonsetc[11597:a0b] index: 2147483647 & length _theArray: 52
2013-09-29 21:09:10.852 testButtonsetc[11597:a0b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2147483647 beyond bounds [0 .. 51]'

如果能得到任何帮助,我将不胜感激。

最佳答案

2147483647 是 NSNotFound。使用 indexOfObjectPassingTest: 时,您应该测试 NSNotFound

来自文档:枚举 {NSNotFound = NSIntegerMax};

- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

返回值数组中对应值通过 predicate 指定的测试的最低索引。如果数组中没有对象通过测试,则返回 NSNotFound

关于ios - NSMutableArray 索引变得非常高并且 removeObjectAtIndex 超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com