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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:24:21+00:00 2026-05-28T03:24:21+00:00

I have a very strange error using NSMutableArray in cocos2d/xcode In my code, I

  • 0

I have a very strange error using NSMutableArray in cocos2d/xcode

In my code, I have a NSMutableArray containing object A

NSMutableArray *peasantArray;

and another NSMutableArray contain object B

NSMutableArray *treeArray;

in the scheduled update function, I called another function which is essentially the following functionality:
I would loop through all the object A inside the *peasantArray, and if any peasant object has a variable var == GameEntityCommandIdling, I would modify the object B in the second NSMutableArray *treeArray,

The issue is that I notice sometimes after I modified the object A inside the peasantArray, the modified variable (var) is being modified/updated inside the object A after by printing out the variable status in a scheduled fashion; but if I am to loop through the NSMutableArray *peasantArray again in the next schedule (1/30s), I will again find the object A with the older/un-updated variable (var), and this is causing my algorithm to be wrong,

However, if I loop through the NSMutableArray *peasantArray less than 1second, each time I would see the variable (var) for object A correctly as the updated variable value,

So is there a limit on how fast I can iterate over the NSMutableArray?

here are some piece of code that I basically just mentioned,

NSMutableArray *peasantArray;
NSMutableArray *treeArray;

…..

peasantArray = [[[NSMutableArray alloc] initWithCapacity:1]retain];
for(int i = 0; i < 1; i++)
{
    Peasant *A = [[Peasant alloc] init];
    [peasantArray addObject:A];
}

….
//inside the update()

for (int i = 0;i < [peasantArray count];i++)
{
    Peasant *A = [peasantArray objectAtIndex:i];
    if (A.status == something)
    {
         printf("A.status is something\n");
         A.status = sometingelse;

        ...
         //update other things not related to this NSMutableArray or object
    }
}

…..

SO the error is that sometimes I will have multiple printf of “A.status is something”, although only one of it should be printed,

I really appreciate any help,

Thanks,

  • 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-28T03:24:22+00:00Added an answer on May 28, 2026 at 3:24 am

    So is there a limit on how fast I can iterate over the NSMutableArray?

    Definitely no. That would be the stupidest implementation of an array I’d ever encountered. 🙂

    The simplest explanation is usually the answer. You say the message is printed more than once. Well have you checked that maybe more than one Peasant has its status set to something?

    If that’s not the case, are you certain the status update is printed twice in the same frame, and not twice in two subsequent updates?

    Earlier on you seemed to indicate that the effect of iterating over one array and modifying the other array’s objects is invalidated somehow. That made me want to point out that if you have the same object in both arrays, modifying the object’s properties in array A will also modify the properties of the same object contained in array B.

    You may want to give breakpoints a(nother) try. The problem should be easy to locate when you step through the code.

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

Sidebar

Related Questions

Using the standard add record code I am getting a very strange error when
I have a very strange error in my Qt project. Here is the code,
it is very strange, because this error doesn't happen all the time... I have
I have a very strange bug, which I believe is caused by some code
A very strange error: if I add some specific code to my project, any
I have a very strange error happening in an App that has been working
I'm using Kohana 3.1 and I'm getting a very strange error. The Kohana POST
I have very strange problem. I'm using IIS 7.0 Integrated mode for my application.
I have a very strange issue regarding only IE8 + Windows Vista, the error
We have a very strange error occurring at a developer site which we are

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.