设置属性是否与释放属性相同? [英] Is setting a property to nil same as releasing the property?

查看:127
本文介绍了设置属性是否与释放属性相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

self.myObject = nil;

[myObject release];

另外,为什么前者通常用于viewDidUnload方法而后者用于dealloc方法?

Also, why is the former generally used in the viewDidUnload method and the latter used in the dealloc method?

推荐答案

假设保留属性,那么:

self.myObject = nil;

将释放对象并将属性的值设置为 nil 。它也将通过setter方法执行此操作,可能会触发KVO通知。这个:

will both release the object and set the value of the property to nil. It will also do this through the setter method, possibly triggering KVO notifications. And this:

[myObject release];

释放对象,在属性中留下悬空指针。

will only release the object, leaving a dangling pointer in the property.

后者通常用于 -dealloc ,因为你不关心变量之后的变量值对象已被释放,也因为您不希望从正在销毁的对象触发KVO通知。前者几乎用在其他任何地方。

The latter is normally used in -dealloc because you don't care about the value of the variable after the object has been deallocated, and also because you would rather not trigger KVO notifications from an object that's being destroyed. The former is used pretty much everywhere else.

这篇关于设置属性是否与释放属性相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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