I'm developing on the iPhone and I'm missing something about storing objects in an NSMutableArray - what am I doing wrong and how should I approach this?
The relevant sections of the code are
I have an object, Entity, defined which inherits directly from NSObject. In my class GLView I wish to define an array of these - so GLView.h
@interface GLView : UIView
{
BOOL controllerSetup;
// Entity etc - first development task it to get one up
NSMutableArray *arrEntities;
}
@property(nonatomic, assign) NSMutableArray *arrEntities;
and in the body in the drawView function - which is called multiple times by a timer event - I allocate the array and, for initial development, just one Entity object and add it to the array
- (void)drawView
{
if(!controllerSetup) { // initialize on first call
[controller setupView:self];
controllerSetup = YES;
arrEntities = [NSMutableArray array];
Entity* thisEntity = [[Entity alloc] initEntity];
[arrEntities addObject:thisEntity];
}
Entity *entity = [arrEntities objectAtIndex:0];
[entity tickEntity:0.1];
}
My understanding was that on the first call of drawView
the NSMutableArray would be set up and the instance of Entity added to it, then in the body the a pointer to first copy of entity stored in the array retrieved and this manipulated (obviously I intend to add more and loop over them later).
However what happens it that on the first call of the procedure everything works fine, the objectAtIndex
retrieves the entity pointer and all works. However on the second call when controllerSetup
is true and the initialization clause not executed arrEntities
is empty and the program crashes as objectAtIndex
retrieves nil.
So what am I doing wrong? Why does my stored Entity object not persist in the NSMutableArray?