1
如果我宣布没有财产声明的伊娃,并且这个伊娃可以在物品生命周期中使用或不使用,我是否必须在dealloc
?伊娃和财产释放dealloc
我有时看到属性被声明为ivar和属性,有时只有属性声明。有什么不同?哪种方法更好?
例子:
@interface MyClass: NSObject
{
NSObject *ivar; // This is sometimes omitted.
}
@property (nonatomic, retain) NSObject *ivar;
@implementation MyClass
@synthesize ivar;
...
-(void)dealloc
{
[ivar release];
[super dealloc];
}
怎么会伊娃声明有时省略?
另一种情况是在没有财产申报:
@interface MyClass: NSObject
{
NSObject *ivar;
}
@implementation MyClass
-(void)thisMethodCanBeCalledOrNot
{
ivar = [[NSObject alloc] init];
[ivar useIt];
//ivar must be alive for further uses in different methods of this class. For this is not released in this method.
}
...
-(void)dealloc
{
[ivar release]; //If thisMethodCanBeCalledOrNot is never called, could this cause a over release in ivar?
[super dealloc];
}
你应该在第二块代码中使用'self.ivar = nil;'而不是直接释放伊娃。 – 2012-03-21 10:11:04