Dealloc method in iOS and setting objects to nil

后端 未结 4 1138
礼貌的吻别
礼貌的吻别 2020-12-13 05:22

I have a pretty basic question. In some examples I\'ve seen, objects are just released in the dealloc method. In others, the objects are released and then set to nil

4条回答
  •  臣服心动
    2020-12-13 06:01

    If there's a dealloc call in more than one place, setting the object variable to nil makes sure it won't be deallocated more than once by mistake. Same logic if the function with the dealloc is called from more than one place, or can be called arbitrarily by external code (i. e. other classes).

    In real life, this typically happens when the enclosing object is "reusable" - supports multiple rounds of content initialization/teardown. The nil-ness of object pointers then becomes a valuable component of object state - it means that the object is "empty" right now.

    Sorry for the generalities.

提交回复
热议问题