gpt4 book ai didi

iphone - NSGenericException,NSMutableArray 给出了一些问题

转载 作者:行者123 更新时间:2023-12-01 17:19:58 24 4
gpt4 key购买 nike

我正在使用 NSMutableArray 来存储在我的 View 中显示的一些 UIView 对象。我正在使用 NSTimer 连续调用一个方法来检查该数组的内容并收到一些错误。

这是控制台的堆栈跟踪

2011-03-15 15:23:26.556 something[8166:207] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x5d32140> was mutated while being enumerated.(
"<PDColoredProgressView: 0x5a1c910; baseClass = UIProgressView; frame = (10 98; 20 20); transform = [0, -1, 1, 0, 0, 0]; opaque = NO; tag = 5; layer = <CALayer: 0x5a1a3e0>>"
)'
*** Call stack at first throw:
(
0 CoreFoundation 0x023a0919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x024ee5de objc_exception_throw + 47
2 CoreFoundation 0x023a03d9 __NSFastEnumerationMutationHandler + 377
3 something 0x00005484 -[somethingViewController renderView] + 1361
4 Foundation 0x0005ac99 __NSFireTimer + 125
5 CoreFoundation 0x02381d43 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
6 CoreFoundation 0x02383384 __CFRunLoopDoTimer + 1364
7 CoreFoundation 0x022dfd09 __CFRunLoopRun + 1817
8 CoreFoundation 0x022df280 CFRunLoopRunSpecific + 208
9 CoreFoundation 0x022df1a1 CFRunLoopRunInMode + 97
10 GraphicsServices 0x02c052c8 GSEventRunModal + 217
11 GraphicsServices 0x02c0538d GSEventRun + 115
12 UIKit 0x002d2b58 UIApplicationMain + 1160
13 something 0x00001ef4 main + 102
14 something 0x00001e85 start + 53
)
terminate called after throwing an instance of 'NSException'

这是导致该错误的行。
if(mutableArray.count != 0)
{
for(PDColoredProgressView *temp in mutableArray)// <- this is the line where the error is occurring.
{
if(temp.progress == 0.0f)
{
[temp removeFromSuperview];
[mutableArray removeObject:temp];
}
}
}

PS。 PDColoredProgressViewUIProgressView 的子类
我正在使用上面的类对象来显示进度,然后我将其从我的 View 中删除。

问。
关于如何纠正我的错误的任何建议。
有没有人遇到过这样的异常。需要帮助!!!!!!!!
提前致谢。

最佳答案

类似于@7KV7 的回答。最好的方法是记下要删除的对象的索引(在 NSIndexSet 中),然后在迭代完成时调用 removeObjectsAtIndex。

因此,使用您的代码,例如(未经测试):

NSMutableIndexSet *indexes;
int count =0;

if(mutableArray.count != 0)
{
for(PDColoredProgressView *temp in mutableArray)// <- this is the line where the error is occurring.
{
if(temp.progress == 0.0f)
{
[temp removeFromSuperview];
[indexes addIndex:count];
}
count++;
}
[mutableArray removeObjectsAtIndexes:indexes];
}

关于iphone - NSGenericException,NSMutableArray 给出了一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5310991/

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