我有这样的代码:
我的类.h
@interface MyClass : CCLayer {
}
@property (nonatomic, retain) CCSprite *spriteName; //retain count = 1
@property (nonatomic, retain) CustomClass *customVariable; //retain count = 1
@end
我的班级.m
@implementation MyClass
@synthesize spriteName;
@synthesize customVariable;
//rough init method
-(void)init
{
self.spriteName = [CCSprite spriteWithFileName:@"a.png"]; //retain count = same
self.customVariable = [[CustomClass alloc] init]; //retain count = 2
}
-(void)dealloc
{
[self.spriteName release]; //retain count = 0
self.spriteName = nil;
[self.customVariable release]; //retain count = 1?
self.customVariable = nil;
[super dealloc];
}
我对此有一些疑问:
1) 我有一个 CCSprite,我已经将它做成了一个属性,但是当我将它分配给 时[CCSprite spriteWithFileName:@"a.png"]
,它是一个自动释放的对象。但是因为我有@property (nonatomic, retain) CCSprite *spriteName
,我必须释放它是吗?
2)对于我的customVariable,以上述方式释放它时似乎出现错误,但是当我将它们更改为下划线时[_customVariable release]; _customVariable = nil
,我没有收到错误。为什么会这样,两者有什么区别?
3)我释放这些对象对吗?我在保留计数中发表了评论,我认为我无法理解。如果有分配或保留,我知道基本知识,那么我应该 +1,但在属性方面我很困惑。