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