问题
What is the difference between these two code snippets:
object = nil;
[object release]
Vs
[object release];
object = nil;
which is the best practice ?
回答1:
object = nil;
[object release]
Don't do that. You are sending a release
message on a nil object that will just do nothing. But the object that was referenced by your object is still in memory because it has never received a release
message.
[object release];
object = nil;
Here you release the object, and for convenience and security, you set nil
to its reference. So you can call (by mistake of course :-) ) any method on that object and the app won't crash.
But if you use a retained property @property(nonatomic, retain)
, calling :
self.object = nil;
equals to call :
[object release];
object = nil;
来源:https://stackoverflow.com/questions/8150896/what-is-the-difference-between-setting-object-nil-and-object-release-vs-obj