开发者

Adding items to array doesn't seem to work

开发者 https://www.devze.com 2023-03-21 09:57 出处:网络
I have written a function that adds a ball to an array yet when I check the count it hasnt increased, please can someone advise.

I have written a function that adds a ball to an array yet when I check the count it hasnt increased, please can someone advise.

NSMutableArray *_otherBalls;


-(void)addBall{
    CCSprite *target = [CCSprite spriteWithFile:@"redbouncyball.gif" rect:CGRectMake(0, 0, 27, 40)]; 

    [self addChild:target];

    //add to our array
    [_otherBalls addObject:target];

    NSLog(@"Added 开发者_如何学Cball : %@",[_otherBalls count]);
}

The log comes out as

Added ball : (null)


From your code it seams that you have never initialized the array. So it' cant store anything. Unless you did in init, but thats hard to guess.


Try

 NSLog(@"Added ball : %lu",[_otherBalls count]);

count is an integer, not an NSObject.


Is _otherBalls an NSMutableArray? Did you make sure to alloc and init _otherBalls?

EDITED: to reflect edited question


NSLog(@"Added ball: %d", [_otherBalls count]); 

Try this because you are counting objects in an array and the result would be integer.
If the problem persists then it means that you havent initialized your array

_otherBalls = [[NSMutableArray alloc] init];
0

精彩评论

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