为什么要释放你已经设置为 nil 的属性? [英] Why release a property that you've already set to nil?

查看:59
本文介绍了为什么要释放你已经设置为 nil 的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是来自 Apple 教程的视图控制器中的两个方法:

Here are two methods in a view controller from an Apple tutorial:

- (void)viewDidUnload {
    self.eventsArray = nil;
    self.locationManager = nil;
    self.addButton = nil;
}

- (void)dealloc {
    [managedObjectContext release];
    [eventsArray release];
    [locationManager release];
    [addButton release];
    [super dealloc];
}

不能将 dealloc 方法缩短为以下内容吗?如果没有,为什么不呢?

Couldn't the dealloc method be shortened to the following? If not, why not?

- (void)dealloc {
    [managedObjectContext release];
    [super dealloc];
}

推荐答案

- (void)viewDidUnload 不能保证被调用,所以你也应该总是在 dealloc 中释放东西.

- (void)viewDidUnload is not guaranteed to be called, so you should always release things in dealloc too.

请参阅这个问题以了解何时调用它,以及您应该做什么什么时候.

See this question to find out when it's called, and what you should do when it is.

这篇关于为什么要释放你已经设置为 nil 的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆