views:

96

answers:

5

Hi Guys,

I am getting the Collection was mutated while being enumerated exception when I am using this code can any one suggest me how to get out of this.

PaymentTerms * currentElement;
for (currentElement in termsArray)
{
    printf("\n currentElement Value........%s",[currentElement.days UTF8String]);
    printf("\n Str value...%s",[Str UTF8String]);
    NSRange range = [currentElement.days rangeOfString:Str options:NSCaseInsensitiveSearch];
    if(!(range.location != NSNotFound))
    {
        PaymentTerms *pTerm1 = [[PaymentTerms alloc]init];
        pTerm1.days = Str;
        printf("\n  pTerm1.days...%s",[ pTerm1.days UTF8String]);
        [termsArray addObject:pTerm1];
    }   
}

Hope I get quick response from ur side. Thank in advance, Monish.

+3  A: 

This line [termsArray addObject:pTerm1]; will throw that exception. You CANNOT add/delete an element from an array inside a for each loop. for (currentElement in termsArray)

vodkhang
A: 

you are adding an object to your collection as you are looping over it, thats whats causing the error. your if statement is nested inside the for

Jesse Naugher
A: 

The error occurs because you are adding new objects to termsArray within the for loop

  • Create a new empty array (e.g.newTermsArray)
  • In the first loop create and add these new items to newTermsArray
  • Then you will need a second loop to add the items from newTermsArray back into the original termsArray
nonnb
+6  A: 

You cannot change array while you're enumerating it. As a workaround you should accumulate new objects in temporary array and add them after enumeration:

PaymentTerms * currentElement;
NSMutableArray* tempArray = [NSMutableArray array];
for (currentElement in termsArray)
{
    NSRange range = [currentElement.days rangeOfString:Str options:NSCaseInsensitiveSearch];
    if(!(range.location != NSNotFound))
    {
       PaymentTerms *pTerm1 = [[PaymentTerms alloc]init];
       pTerm1.days = Str;
       [tempArray addObject:pTerm1];
       [pTerm1 release];
    }   
}
[termsArray addObjectsFromArray: tempArray];

P.S. do not forget to release pTerm1 object you create - your code contains memory leak

In respond to poster's comment (and actual task) - I think the easiest way to make bool flag indicating if day value was found in cycle. If not - add new object after cycle ends:

PaymentTerms * currentElement;
BOOL dayFound = NO;
for (currentElement in termsArray)
{
    NSRange range = [currentElement.days rangeOfString:Str options:NSCaseInsensitiveSearch];
    if(range.location != NSNotFound)
        dayFound = YES;
}
if (!dayFound)
     // Create and add new object here
Vladimir
@vladimir:Thanks for your useful code.but I got one more problem.Actually the thing is I need to check the string the array if it is not present I have to add it to the array list.Now the problem is I had 3 Items in my list which is not equal to the string I am searching for so It was adding the object 3 times instead of 1 time.Is there any other suggestions for this.
You don't need to release tempArray here because you never take ownership of it. Also, `!(range.location != NSNotFound)` is the same as `(range.location == NSNotFound)`.
JeremyP
@vodkhang, no doing so you'll just add a new element if any of objects in termsArray has day property different from Str
Vladimir
@Vladimir : ops, damn it. It is a stupid mistake:). Your new one is the correct one
vodkhang
+1  A: 

Yes...we cannot enumerate while the array is getting updated...This might be irritating for the programmers who are from ActionScript background.Some times things go worse like "You even dont get a crash or intimation at runtime when you update an array count while it is being enumerated"-The execution just behaves abnormally at that time.

Btw you can go for this type of implementation where you can have minor changes to your code.

 for (int i=0 ; i< termsArray.count ;i++)  //counting termsArray on every iteration
 {
  id currentElement  = [ termsArray objectAtIndex:i];
  ......
  .....
 }

Of-course,This(i< termsArray.count) might seem bad as we are calculating the count for every iteration...And thats the trick here to have minor changes.But I would strongly recommend VLADIMIR's implementation as its clear for reading.

Man With Steel Nerves...