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 9008347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:54:59+00:00 2026-06-16T01:54:59+00:00

I have an NSMutableArray that is holding a list of objects. What I am

  • 0

I have an NSMutableArray that is holding a list of objects. What I am trying to do is iterate through this list of objects, and find the matching object for the I am trying to insert. Once I find the matching object, I then want to simply replace the object that is currently in the list with the one I am trying to insert. I am trying to do this using fast enumeration:

TestResult *result = [[TestResult alloc] init];
    [result setName:name];
    [result setScore:score];
    [result setDateStamp:date];


    for (TestResult *checkTest in [DataModel sharedInstance].testResultList) {

        NSInteger indx = [[DataModel sharedInstance].testResultList indexOfObjectPassingTest:^BOOL(TestResult *obj, NSUInteger idx, BOOL *stop) {
            return [obj.name isEqualToString:name];
        }];

        if (indx != NSNotFound) {

            [[DataModel sharedInstance].testResultList replaceObjectAtIndex:indx withObject:result];

        }

    }

Unfortunately, when I run the above code, I am getting the following error:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x9624820> was mutated while being enumerated.'

Can anyone see what I am doing wrong, and how can work around this, yet achieve the functionality that I described above?

  • 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-06-16T01:54:59+00:00Added an answer on June 16, 2026 at 1:54 am

    First of all the crash is pretty self explanatory.
    You are actually mutating (aka. replacing an object in the array), while the fast enumeration is still in progress, which is not permitted.

    The solution if you would go with your design would be to actually capture the index of the object, break the fast enumeration and replace the object outside the fast enumeration.

    However what you are doing is not correct.
    The way to use the indexOfObjectPassingTest is this:

    NSInteger indx = [[DataModel sharedInstance].testResultList indexOfObjectPassingTest:^BOOL(TestResult *obj, NSUInteger idx, BOOL *stop) {
        return [obj.name isEqualToString:name];
    }];
    
    if (indx != NSNotFound) {
    
        [[DataModel sharedInstance].testResultList replaceObjectAtIndex:indx withObject:result];
    
    }
    

    You don’t need to manually enumerate through all elements of the array.
    The function does this for you internally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NSMutableArray that has custom objects in it. Each object does have
I have an NSMutableArray that contains a list of objects of type MyClass, with
I have an NSMutableArray that contains objects of type Person. The Person object contains
I have an 3 NSMutableArray objects that contain CMTime objects. How can I iterate
I have an NSMutableArray that contains objects of type Person. The Person object contains
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have been working with NSArray s and NSMutableArray s that store NSDate objects
I have an NSMutableArray that I'm trying to store and access some structs. How
I have an NSMutableArray that I display through a table view. The problem is
I have two simple NSMutableArray that consists of few objects. Some of these objects

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.