NSMutableArray 在运行时崩溃
当尝试访问正确加载的 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 的自动释放功能可能是问题所在吗?
I'm getting a crash at runtime when try to access an NSMutable array that is correctly loaded.
here is the code
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
Please somebody explain me why is this happening.
Could the autorelease feature of the NSMutableArray be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(根据请求重新发布)
如果您的数组 gameItems 是一个成员(看起来确实如此),以便能够在其他函数(例如 callEveryFrame)中访问它,那么您肯定需要以这种方式初始化它:
gameItems = [[ NSMutableArray alloc] init]
;(我认为你错过了分配)
(reposted on request)
If your array gameItems is a member, which it seems it is, to be able to access it in other functions such as callEveryFrame then surely you need to have initialised it in this way:
gameItems = [[NSMutableArray alloc] init]
;(you missed the alloc I reckon)