开发者

Objective-C style question: do "release" or "nil" properties in dealloc? [duplicate]

开发者 https://www.devze.com 2023-02-01 21:57 出处:网络
This question already has answers here: Closed 12 years ago. 开发者_如何学GoPossible Duplicates: Why shouldn't I use Objective C 2.0 accessors in init/dealloc?
This question already has answers here: Closed 12 years ago.
开发者_如何学Go

Possible Duplicates:

Why shouldn't I use Objective C 2.0 accessors in init/dealloc?

dealloc, use release or set to nil for properties?

Hi,

Apple usually release ivars in dealloc but is there anything wrong with nilling the properties in dealloc?

I mean instead of this:

- (void) dealoc(){
   [myRetainedProperty release];
   [super dealloc];
}

write code like this:

- (void) dealoc(){
   self.myRetainedProperty = nil;
   [super dealloc];
} 

I know that it is one additional method call but on the other hand it is safer as it doesn't crashes when you change your property form retain to assign and forget to amend dealloc.

What do you think? Can you think about any other reason to use release instead of setting to nil besides performance?

UPDATE: It appeared that this question was a duplicate of "Why shouldn't I use Objective C 2.0 accessors in init/dealloc?".


Something quite similar has already been discussed: Why shouldn't I use Objective C 2.0 accessors in init/dealloc?


i was using the "release" style for quite some time, then i switched to setting the properties to nil afterwards. I think if you just release the ivar then you have the chance of reusing it in the dealloc later on, which might not happen but still ... Also i consider that setting the properties to nil conforms more to the "encapsulate your ivars with properties" theory ..

So I consider better using the "setting to nil" style, however i don't know any real reasons for using only release ...

Moszi


Yeah, assign nil looks better, until you will forget to write self. before property name :)

0

精彩评论

暂无评论...
验证码 换一张
取 消