如果創建一個UIViewController子類,方法「的dealloc」自動爲您創建。是否有必要在Objective-C類中添加dealloc方法?
- (void)dealloc{}
然而,當我創建Objective-C類,該方法是不自動創建。是否有必要添加dealloc方法,以便我可以釋放類中的屬性?特別是如果我將它們保留在頭文件中?
舉例來說,在我的Objective-C類的頭文件我有
@interface ClassA : NSObject
{
NSString *someProperty;
UINavigationController *navcon;
}
@property (nonatomic, retain) NSString *someProperty;
@property (nonatomic, retain) UINavigationController *navcon;
我是否需要手動創建一個dealloc方法來釋放下面的屬性?
-(void)dealloc
{
[someProperty release];
[navcon release];
}
不要忘記調用你的dealloc方法添加到[超級的dealloc]! –
請注意,使用ARC時,調用'[super dealloc]'會導致錯誤。編譯器爲你處理它。 – Eric