开发者

Problem adding object to NSMutableArray

开发者 https://www.devze.com 2023-03-30 22:07 出处:网络
I am having problems adding objects to my NSMutableArray.It seems that something gets added (object count increases by 1 in debugger), but the only thing added is a 0x0 (null) instead of the address o

I am having problems adding objects to my NSMutableArray. It seems that something gets added (object count increases by 1 in debugger), but the only thing added is a 0x0 (null) instead of the address of an object. I've read through everything somewhat relevant that I could find, but I couldn't find anything that seemed to answer this issue. Most related posts seem to revolve around memory management, but the solution is not jumping out at me.

I appreciate any help you can provide.

I've included what I think are the relevant parts of the code. Please tell me if you need to see anything more.

GamePlayView.h

@interface GamePlayView : UIViewController 
{
    Player *gamePlayer;
    NSMutableArray *boardObjects;
}

@property (retain, nonatomic) Player *gamePlayer;
@property (retain, nonatomic) NSMutableArray *boardObjects;

@end

GamePlayView.m

- (void)viewDidLoad 
{
    // Create player

    Player *tempPlayer = [[Player alloc] initWithFrame: self.view.frame];
    if (tempPlayer == NULL) {
        NSLog(@"GamePlayView viewDidLoad: null Player");
    }
    else gamePlayer = tempPlayer;

    // Create array of board objects

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

    self.boardObjects = newArray;
    [self.boardObjects addObject: gamePlayer];  // First breakpoint here
    topObject = 0;

    BoardObject *mine = [[BoardObject alloc] initWithFrame: self.view.frame];

    [self.boardObjects addObject: mine];  // Second breakpoint here
    topObject = 1;

    [super viewDidLoad];

} // End viewDidLoad

I put breakpoints at the addObject lines (commented in code). When execution stops at the first breakpoint, the debugger shows a good tempPlayer, and a good gamePlayer (both with the same address). It shows 0 objects in boardObjects, like this:

boardObjects = (_NSArrayM *) 0x4b22080 0 objects

When I step over this breakpoint, the debugger shows 1 object in boardObjects, as follows:

boardObjects = (_NSArrayM *) 0x4b22080 1 objects
    0 = (NSObject *) 0x0

When I continue program execution, and the debugger stops at the next breakpoint, I also see a good mine object, with boardObjects still described as above. After stepping over t开发者_开发问答his breakpoint, boardObjects now looks like this:

boardObjects = (_NSArrayM *) 0x4b22080 2 objects
    0 = (NSObject *) 0x0
    1 = (NSObject *) 0x0


This could be the case that tempPlayer is a local variable, after returning from the function, the local variable is automatically released.


Someone suggested I display the boardObjects in the code anyway, even though it looked from the debugger that I only had null objects. I inserted this code after the last addObject:

NSLog(@"self.boardObjects is: %@", [self.boardObjects description]);

This displayed good objects: the gamePlayer and the mine!

I did verify that immediately before this NSLog statement, and immediately after, the debugger still displays two null entries in boardObjects.

It seems like the answer to my original question, then, is that the code itself is correct. However, it seems now that I have a new question: Does this mean I'll never be able to view into an NSMutableArray from within the debugger?


You should assign @property with self.

self.gamePlayer = tempPlayer;
0

精彩评论

暂无评论...
验证码 换一张
取 消