self.prop = nil; vs. [prop release]; prop = nil; [英] self.prop=nil; vs. [prop release];prop=nil;

查看:115
本文介绍了self.prop = nil; vs. [prop release]; prop = nil;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用的是综合属性,那为什么我不简单地说:

If I am using synthesized properties, then why wouldn't I simply say :

self.property = nil;

这将释放引用计数,并确保我没有悬空的指针.

This will release the ref count, and make sure I don't have a dangling pointer.

看起来很简单,但是我看到的99%的代码似乎都是这样做的:

Seems straightforward, and yet 99% of code I see seems to do it this way:

[property release];
property = nil;

是的,在大多数情况下,它们是属性. 我感到自己缺少了什么东西的可怕感觉? 就像我忘了放自我"一样.在某些属性前面,想知道为什么它崩溃了:-)

and yes, in most cases they are properties. I get the horrible feeling I am missing something? Sorta like when I forgot to put "self." in front of some of the properties and wondered why it was crashing :-)

推荐答案

如果要尝试使用dealloc方法清除任何地方的属性值,那么self.property = nil;是最好的方法.它将为您处理所有内存管理任务,有助于防止出现任何问题.

If you're trying to clear the value of a property anywhere but in a dealloc method, then self.property = nil; is the best way to do it. It'll handle all the memory management tasks for you, helping to prevent any issues.

如果您使用的是dealloc方法,那么释放ivar的最佳方法尚有待商debate,但通常建议使用[property release];,因为调用self.property可能会有次要效果.在dealloc方法中,不需要调用property = nil;.

If you are in the dealloc method, than the best way to release that ivar is up for debate, but [property release]; tends to be recommended, since calling self.property may have secondary effects. In the dealloc method, calling property = nil; is not necessary.

这篇关于self.prop = nil; vs. [prop release]; prop = nil;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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