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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:01:32+00:00 2026-05-28T06:01:32+00:00

I am attempting to populate one NSMutableArray with objects from a second NSMutableArray while

  • 0

I am attempting to populate one NSMutableArray with objects from a second NSMutableArray while inside of a For loop for the second array. When I add an object to the first array I am kicked out of the loop. This is occurring inside of a ‘schedule’ callback in a COCOS2D application.

The weird thing is it works fine in one case but not in another. The first time it works is noted below in the code sample. The second time is also noted in code, with the relevant line denoted with 5 *’s. The only thing that is fundamentally different is that the second example happens over and over inside of a While loop, constantly filling and emptying the second NSMutableArray.

Something is clearly happening that I just don’t understand. I’m hoping that someone will be able to catch it.

Thanks in advance for the help!

The following code is the relevant part of the method:

NSMutableArray *gameSprites = [[NSMutableArray alloc] init];
NSMutableArray *guns = [[NSMutableArray alloc] init];
NSMutableArray *newGuns = [[NSMutableArray alloc] init];

// START OF THE FIRST EXAMPLE 

// collect sprites that are guns
for (LPSprite *sprite in gameSprites)
{
    if ([sprite isGun])
    {
        [guns addObject:gun];
    }
}

// END OF THE FIRST EXAMPLE


// do work for all guns in gamesprite collection
int numGuns = [guns count];

while (numGuns > 0)
{

    // START OF THE SECOND EXAMPLE

    for (LPGun *gun in guns)
    {

        if (special condition that only occurs sometimes)
        {       
            LPGun *portalGun = [[LPGun alloc] init];

            // ***** When this line is executed, I get 'kicked' out of the method and execution starts again at the beginning of the method. No exception is thrown or noticed.
            [newGuns addObject:portalGun];
        }
    }

    numGuns = [newGuns count];

    // empty gun collection and add any 'new' guns to the collection so the above work can be repeated
    [guns removeAllObjects];

    for (LPGun *g in newGuns)
    {
        [guns addObject:g];
    }

    [newGuns removeAllObjects];

    // END OF THE SECOND EXAMPLE
}
  • 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-28T06:01:33+00:00Added an answer on May 28, 2026 at 6:01 am

    As mentioned in one of my comments above:

    Adding some logging helped. It forced me to look at the console log which was actually spewing out an error (or maybe a warning) that was preventing the object from being added to the array. The init line just above the add was failing because a texture could not be found.

    So I misinterpreted the symptoms, and as a result the topic here may be a bit misleading to people visiting it in the future.

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

Sidebar

Related Questions

I'm attempting to populate a drop down from an Nhibernate object, but having problems
I am attempting to insert a copy of a row from one table into
i'm attempting to populate an array with random numbers, but the random numbers must
While attempting to populate the items in a ListView following the outcome of a
I am attempting to populate a list of records within the last X seconds
Attempting to print out a list of values from 2 different variables that are
Attempting to deploy a MOSS solution to a UAT server from dev server for
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to create a SQLite db and populate it with data initially. When
I am attempting to figure out if a column in Oracle is populated from

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.