views:

259

answers:

1

In my program I have two classes: Collector and Entity. Collector stores Entities in NSMutableArray.

@interface Entity : NSObject {
    ...
}

@interface Collector : NSObject {
    NSMutableArray *entities;
    uint someInt;
    ...
}
- (void)addEntity:(Entity*)newEntity;   // implemented as [entities addObject:newEntity];

Collectors are also stored in NSMutableArray (let's call it collectors). Then I have such code (something like this):

for (uint i = 0; i < [collectors count]; i++) {
    Collector *curCollector = [[collectors objectAtIndex:i] retain];

    Entity *curEntity = [[Entity alloc] init];
    [curCollector addEntity:curEntity];
    [curCollector setSomeInt:50];

    [curEntity release];
    [curCollector release];
}

After execution of this code part, I have no new entities inside collectors, but someInt value changes correctly. Moreover, when I debug my code, on step before releasing of variables, curCollector contains new entity and has address, same as one of objects inside collectors array, but that object (in collectors array) has no entities.

What am I doing wrong?

P.S.:
During debug I checked all objects - no nils found (especially for entities).
About addObject - all implementation code is [entities addObject:newEntity];
And another interesting thing (to point in in my explanation above) - int value is set correctly (objects of the collectors' array contain someInt == 50).

P.P.S: I've added a line before releasing code parts:

[collectors replaceObjectAtIndex:i withObject:curCollector];

No results!!

A: 

Hmm. I'm so sorry for wasting your time... There is no errors in this code.
This code (of course) was a part of the program. And the error was found some steps later.
The problems during debug process were provoked by XCode debugger, which can't look deeply in class's contents. I mean that entity was in entities array. That array was inside collector and this collector was inside collectors array itself. And when I tried to get entities array to some local variable, returned array had entities (displayed in XCode debugger).

kpower