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

  • Home
  • SEARCH
  • 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 8004761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:00:46+00:00 2026-06-04T17:00:46+00:00

I have a problem with three clases. The the first class is called Player

  • 0

I have a problem with three clases. The the first class is called Player. This class has an NSMutableArray inside it called units. This array is made up of objects of the class Unit. This class in turn, has an NSMutableArray called bullets. It works like this:

At a certain point the class Player (it could just be the ViewController instead) adds an object to the units. Then, when an instance of Unit is initialized, as a result of the above, it creates an NSTimer that is in charge of creating bullets every second.

The thing is, is crashes somewhere in the middle of this with a SIGABRT that tells me that there was an exception because: Collection <__NSArrayM: 0xb1a2970> was mutated while being enumerated. Also, I took away the line that created bullets and it stops crashing, proving that is the problem. What does that mean!

Here is a bit of executable code that might work:

ViewController.h (instead of player)

@interface ViewController : UIViewController
{
    NSMutableArray *units;
    NSTimer *updateTimer;
}
-(void)Update;

ViewController.m

@implementation ViewController
//methods...

- (void)viewDidLoad
{
    //more default code

    //Initialized array and adds one object with the default constructor for simplicity
    units = [[NSMutableArray alloc] initWithObjects:[[Unit alloc] init], nil]
}

-(void)Update
{
    for(Unit *unit in units)
    {
        [unit Update];
        if(unit.deleteFromList)
            [units removeObject:unit];   
    }
}
//More methods
@end

Unit.h

@interface Unit : NSObject
{
    NSMutableArray *bullets;
    NSTimer *bulletTimer;
    boolean deleteFromList;
}

@property(readonly, assign)deleteFromList;

-(void)Fire;

-(void)Update;

Unit.m

@implementation Unit

@synthesize deleteFromList;

-(id)init
{
    if(self)
    {
        bullets = [[NSMutableArray alloc] init];
        bulletTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(fire) userInfo:NULL repeats:true];
        deleteFromList = false;
    }

    return self;
}

-(void)Fire
{
    [bullets addObject:[[Bullet alloc] init]];
}

-(void)Update
{
    for(Bullet *bullet in bullets)
    {
        [bullet Update];
        if(bullet.deleteFromList)
            [bullets removeObject:bullet];
    }

    if(certainCondition)
        deleteFromList = true;
}

The bullet class will be omitted because the contents are irrelevant to what happens. Also, all the classes and constructors were shortened because the rest is useless for this example

EDIT:

Another thing I forgot to add is that the timer is created in an enumeration of the NSMutableArray units in the update method i’m about to add. I’m also adding a variable to Unit and Bullet that orders it to delete. The bullet update changes the position and also changes the deleteFromList variable

  • 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-04T17:00:47+00:00Added an answer on June 4, 2026 at 5:00 pm

    You can’t remove any item in NSMutableArray while for-loop or enumerating it.

    Document: It is not safe to modify a mutable collection while enumerating through it. Some enumerators may currently allow enumeration of a collection that is modified, but this behavior is not guaranteed to be supported in the future.

    for(Bullet *bullet in bullets)
    {
        [bullet Update];
        if(bullet.deleteFromList)
            [bullets removeObject:bullet];
    }
    

    to

    NSMutableArray *toRemove = [NSMutableArray array];
    for(Bullet *bullet in bullets)
    {
        [bullet Update];
        if(bullet.deleteFromList)
            [toRemove addObject:bullet];
    }
    [bullets removeObjectsInArray:toRemove];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the next problem: TabControl has three TabPages. Every TabPage has its own
I have three tables being used for this problem: songs, blacklist, and whitelist. The
I don't know where to look for about this problem ... I have three
I have a template class that I have made called hash . My template
I have a TabActivity that has three tabs. The fist tab is the problem.
I have a class called Stream that has many records in the database, and
I have the following problem. I have three classes, A, B and C. A
Im having a problem with python.. I have a binary tree node type: class
I've encountered some problems with MongoID. I have three models: require 'mongoid' class Configuration
I have a problem where I have to add thirty-three integer vectors of equal

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.