dealloc,使用释放或设置为nil的属性? [英] dealloc, use release or set to nil for properties?
问题描述
当我们在实现dealloc的时候,在书中和网上看到各种各样的代码时,我注意到了一些混乱。我的问题是当使用@property我应该使用以下哪一个。直到现在我一直在使用VERSION_001。
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.
@property(nonatomic, retain) NSString *name;
@property(nonatomic, retain) NSString *type;
@property(nonatomic, retain) NSString *payload;
@property(nonatomic, retain) NSString *orbit;
VERSION 001
VERSION 001
- (void)dealloc {
[name release];
[type release];
[payload release];
[orbit release];
[super dealloc];
}
VERSION 002
VERSION 002
- (void)dealloc {
[self setName:nil];
[self setType:nil];
[self setPayload:nil];
[self setOrbit:nil];
[super dealloc];
}
推荐答案
除非您不使用 @synthesize
关键字。当您将属性设置为nil时,它们将由安装程序在后台释放。现在,与第一版本相比,可能会有轻微的性能提升,因为在后台没有太多需要发生的事情(例如指针比较,一切苹果隐藏在幕后)。我总是使用版本001,因为它是更多的点和未来的开发人员不必挖了解我的代码,以弄清楚我完成了什么。
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.
这篇关于dealloc,使用释放或设置为nil的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!