开发者

Is it necessary to add a dealloc method in a Objective-C Class?

If a UIviewController subclass is created, the method 'dealloc' is created automatically for you.

- (void)dealloc{}

However, when I create a Objective-C Class, the method is not auto-created. Is it necessary to add the dealloc method so that I can release the properties in the class? Especially if I have retained them in the header file?

For example in my Objective-C class header file I have

@interface ClassA : NSObject 
{
    NSString *someProperty;
    UINavigationController *navcon;
}

@property (nonatomic, retain) NSString *someProperty;
@property (nonatomic, retain) UINavigationController *navcon;

Do I need to manually create a dealloc method to release the properties as bel开发者_开发百科ow?

-(void)dealloc
{
    [someProperty release];
    [navcon release];
}


Yes you must.

dealloc is called upon your object before it is destroyed for good and its memory is returned to the OS. If it holds onto other objects, such as in your case, it is important for those objects to be released too.

That's why you need to override dealloc and release any resource you are holding to there.

The fact that some Xcode template may or may not provide you with a sample implementation of dealloc is irrelevant.


You're responsible for releasing all of the top-level objects in the view controller's nib file. -dealloc is a common place to do so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜