What is the difference between setting object = nil and [object release] VS [object release] and object = nil?

别等时光非礼了梦想. 提交于 2020-01-13 05:46:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!