Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

iphone - 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.

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

VERSION 001

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

VERSION 002

- (void)dealloc {
    [self setName:nil];
    [self setType:nil];
    [self setPayload:nil];
    [self setOrbit:nil];
    [super dealloc];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...