dealloc,使用release或設置為nil屬性?

[英]dealloc, use release or set to nil for properties?


I have noticed a little confusion when looking at various bits of code both in books and on the web when it comes to implementing dealloc. My question is when using @property which of the following should I be using. Up until now I have been using VERSION_001.

在實現dealloc時,我在書籍和網絡上查看各種代碼時都注意到了一點混亂。我的問題是在使用@property時我應該使用以下哪個。到目前為止,我一直在使用VERSION_001。

@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) NSString *type;
@property(nonatomic, retain) NSString *payload;
@property(nonatomic, retain) NSString *orbit;

VERSION 001

版本001

- (void)dealloc {
    [name release];
    [type release];
    [payload release];
    [orbit release];
    [super dealloc];
}

VERSION 002

版本002

- (void)dealloc {
    [self setName:nil];
    [self setType:nil];
    [self setPayload:nil];
    [self setOrbit:nil];
    [super dealloc];
}

1 个解决方案

#1


17  

There is no real difference, Unless you are not using the @synthesize keyword. When you set the properties to nil, they are being released behind the scenes by the setter. Now, there may be a slight performance increase over the fist version, because not so much needs to happen behind the scenes (e.g. pointer comparison, and everything else apple hides behind the scenes). I always use version 001, because it is more to the point and future developers don't have to dig though my code to figure out what I am accomplishing.

除非您沒有使用@synthesize關鍵字,否則沒有什么區別。當您將屬性設置為nil時,它們將由setter在幕后釋放。現在,第一版可能會有輕微的性能提升,因為不需要在幕后發生(例如指針比較,以及蘋果隱藏在幕后的其他所有內容)。我總是使用版本001,因為它更加重要,未來的開發人員不必挖掘我的代碼來弄清楚我正在完成的事情。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/11/08/72a1f86b3d15011450111ce1944cc371.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com