わかった。これをもう一度言います。アクセサーはあなたの友達です。それらを使用してください。いつも。注:はい、Appleがinitとdeallocでそれらを使用しないことを推奨していることは知っていますが、15年間、これが一度も問題を引き起こしたことはなく、使用しないこともあります。この場合のように。使いたくない時もありますが、使いたくない時よりずっと少ないです。)
buttonTextメソッドの場合:
- (void)buttonText:(int)number
{
lifeChange = [NSNumber numberWithInt:number];
NSString *text = [[NSString alloc] initWithFormat:@"%d", number];
CCLabel *label = [CCLabel labelWithString:text fontName:@"Times New Roman" fontSize:20];
label.position = CGPointMake(35, 20);
[self addChild:label];
}
次のことを行う必要があります。
- (void)buttonText:(int)number
{
NSString *text = [[[NSString alloc] initWithFormat:@"%d", number] autorelease];
CCLabel *label = [CCLabel labelWithString:text fontName:@"Times New Roman" fontSize:20];
[self setLifeChange:[NSNumber numberWithInt:number]];
label.position = CGPointMake(35, 20);
[self addChild:label];
}
コードを見て、alloc / copy/retainの必要性とrelease/autoreleaseのバランスをどのように取っているかを理解してください。一見すると、あなたは本当にメモリ管理を台無しにしています。