什么最能解释 [myVar dealloc] 和 [myVar release] 之间的区别?
我想我知道其中的区别,但不知道如何正确解释。
dealloc 立即完全删除该变量保留的内存。
release 将该变量内存的保留计数器递减 -1。 如果它是 1,那么它就是 0,所以此时它与 dealloc 具有相同的效果。
是对的吗? 或者有更好的简短解释吗?
I think I know the difference, but don't know how to explain that correctly.
dealloc removes the memory reserved by that variable totally and immediately.
release decrements the retain counter of that variable's memory by -1. if it was 1, then it's 0, so it would have the same effect as dealloc in that moment.
is that right? or is there an better short explanation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完全正确。
但是在使用对象时,您不会使用dealloc,因为您不知道保留计数是多少。 你也不关心。 您只需通过调用
release
表示您不再需要它。 一旦没有人这样做,该对象就会自行调用dealloc
。That's exactly right.
But you wouldn't use
dealloc
, when using an object, because you don't know what the retain count is. Nor do you care. You just say that you don't need it anymore, by callingrelease
. And once nobody does, the object will calldealloc
on itself.全部正确,但您缺少的一个关键点是您永远不应该自己调用 dealloc。 以下是 Apple 文档中有关 NSObject 的 dealloc 方法的一些信息:(
来自 http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm /NSObject/dealloc)
All correct, but the one key point you're missing is that you should never call dealloc yourself. Here's some information from Apple's documentation on NSObject's dealloc method:
(from http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/dealloc)