开发者

NSGenericException, NSMutableArray giving some problem

I am using a NSMutableArray to store some of the UIView objects that are displayed on my view. I am calling a method continuously using NSTimer to check the what are the contents of that array and received some error.

Here is the stack trace of the console

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'

And here is the line that is causing that error.

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.PDColoredProgressView is a subc开发者_JAVA技巧lass of UIProgressView

I am using the above class objects to show progress and after that I remove it from my view.

Q.

Any suggestions on how to correct my mistakes.

Do anyone have ever come across such exception. Help needed!!!!!!!!

Thanks in advance.


Similar to @7KV7's answer. The best way to do this is to keep a note of the index of the objects you want to remove (in a NSIndexSet) and then when the iteration is complete call removeObjectsAtIndex.

So using your code, something like (untested):

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


I think the fact that you are trying to modify while iterating is causing the problem. Basically you are modifying the list in the loop you are iterating. The line that causes the problem is:

[mutableArray removeObject:temp];

as you are iterating over mutableArray. A possible solution is to accumulate the elements you want to delete in a different list and remove them after the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜