Objective-C Difference between setting nil and releasing

前端 未结 4 1366
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 22:55

I\'ve learned that in dealloc you do [object release]; but in viewDidUnload (in a UIViewController subclass) you do self.object

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 23:33

    If you do [object release], and want to access the object the app simply crash. If you do object = nil, and want to access the object nothing will perform.

    The reason is in the [object release], you are attempted to free the object. so its dont have any pointer (no memoty).In the object = nil, you are attempted to assign the object with null pointer. so if u trying to access the object nothing will happen.

    Generally we wrote the code as [object release]; object = nil; because if u access the object unexpectedly, the app wont crashed.

提交回复
热议问题