尝试访问正确加载的NSMutable数组时,我在运行时遇到崩溃。 这是代码
NSMutableArray *gameItems;
-(id) init
{
if( (self=[super init])) {
//initialize array
gameItems = [NSMutableArray array];
for(int i = 0; i < 3; i++)
{
GI *gameItem = [[GI alloc] init];
gameItem.image = [[CCSprite alloc] initWithFile:@"triangle.png"];
gameItem.Position = ccp(140+40*i,200);
[gameItems addObject:gameItem];
[gameItem release];
NSLog(@"%d",[gameItems count]); //SHOWS THE SIZE OF THE ARRAY INCREMENTING CORRECTLY
}
NSLog(@"%d",[gameItems count]); //show " 3 " correct !
for(GI *gameItem in gameItems)
{
[self addChild:gameItem.image];
NSLog(@"%d",[gameItems count]); //show 3 correct !
}
[self schedule:@selector(callEveryFrame:)];
}
return self;
}
- (void) callEveryFrame:(ccTime)dt
{
NSLog(@"----->%d",[gameItems count]); //CRASHES AT RUNTIME IN THIS LINE
}
@end
请有人解释我为什么会这样。 NSMutableArray的自动释放功能可能是问题吗?
答案 0 :(得分:1)
(根据要求转发)
如果您的数组gameItems是一个成员,它似乎是一个成员,能够在其他函数(如callEveryFrame)中访问它,那么您肯定需要以这种方式初始化它:gameItems = [[NSMutableArray alloc] init]
;
(你错过了我认为的分配)