Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 977321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:53:03+00:00 2026-05-16T03:53:03+00:00

I am getting the Collection was mutated while being enumerated exception when I am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:53:04+00:00Added an answer on May 16, 2026 at 3:53 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Getting exception as “Collection was mutated while being enumerated” This question is
While working on one application I am getting this error: can't convert Enumerator into
I am getting this exception: Collection was modified; enumeration operation may not execute. Directly
I am getting this exception: Collection was modified; enumeration operation may not execute. when
Can anyone conjure from this code why the ItemsSource line would be getting a
Getting this error when try to add an item to my repositories/context: Collection has
I'm stuck in getting the product url in a product collection. I have this
I'm getting the error Illegal access to loading collection when I'm trying to get
I found the MSDN example code for getting the default view of a collection
If I'm in my view getting collection of images like this foreach (var item

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.