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.