プロパティ宣言なしでivarを宣言し、このivarをオブジェクトのライフサイクルで使用するかどうかを指定すると、dealloc
にリリースする必要がありますか?deallocのivarとプロパティのリリース
私はときどき、プロパティが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];
}
IVAR宣言は時々省略されているどのように来ますか?
@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];
}
ivarを直接リリースするのではなく、コードの2番目のブロックで 'self.ivar = nil;'を使うべきです。 –