Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

NSMutableArray addObject not working correctly

Hey,

So I've got the following code, and it seems as though when I try to add the local currentOrganism object to the self.currentGeneration array it's not working correctly and I can't figure out why, when I walk through it with the debugger, currentOrganism is being populated right, but then the xcode log shows that self.currentGeneration objectAtIndex:0 is empty. Oh and the log shows that the memory address of self.currentGeneration is 0x6a224b0, not 0x0, so I'm assuming that means it's initialized correctly? Any help would be greatly appreciated!! :)

-(void)initializeDefaultOrganisms{
int organism;
int gene;

NSMutableArray *currentOrganism = [[NSMutableArray alloc] init];

//initialize normal organisms
for (organism = 0; organism < NUMBER_ORGANISMS; organism++) {
for (gene = 0; gene < NUMBER_GENES; gene++) {
[currentOrganism addObject:[NSNumber numberWithInt:(arc4random()%ALLELES)]];
}

[[self currentGeneration] addObject:currentOrganism];
NSLog(@"currentOrganism has %i objects", [currentOrganism count]);
[currentOrganism removeAllObjects];
}
NSLog(@"current organism has %i objects at index 0, mem address %p",[[self.currentGeneration objectAtIndex:0] count], self.currentGeneration);
}
Post edited by heys61 on

Replies

  • DomeleDomele Posts: 2,948Registered Users
    You remove all objects from currentOrganism. What do you expect?
    If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.



    New app - See screenshots and details at www.globaclock.com.



    If you want to
  • Duncan CDuncan C Posts: 8,031Tutorial Authors, Registered Users
    heys61;418575 said:
    Hey,

    So I've got the following code, and it seems as though when I try to add the local currentOrganism object to the self.currentGeneration array it's not working correctly and I can't figure out why, when I walk through it with the debugger, currentOrganism is being populated right, but then the xcode log shows that self.currentGeneration objectAtIndex:0 is empty. Oh and the log shows that the memory address of self.currentGeneration is 0x6a224b0, not 0x0, so I'm assuming that means it's initialized correctly? Any help would be greatly appreciated!! :)

    -(void)initializeDefaultOrganisms{
    int organism;
    int gene;

    NSMutableArray *currentOrganism = [[NSMutableArray alloc] init];

    //initialize normal organisms
    for (organism = 0; organism < NUMBER_ORGANISMS; organism++) {
    for (gene = 0; gene < NUMBER_GENES; gene++) {
    [currentOrganism addObject:[NSNumber numberWithInt:(arc4random()%ALLELES)]];
    }

    [[self currentGeneration] addObject:currentOrganism];
    NSLog(@"currentOrganism has %i objects", [currentOrganism count]);
    [currentOrganism removeAllObjects];
    }
    NSLog(@"current organism has %i objects at index 0, mem address %p",[[self.currentGeneration objectAtIndex:0] count], self.currentGeneration);
    }

    Why do you have the line in bold red above? What do you expect it to do?
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.