Who calls the dealloc method and when in Objective C?

前端 未结 4 767
深忆病人
深忆病人 2021-02-05 14:06

When a custom class is created in Objective C, when and how is the dealloc method called? Is it something that I have to implement somehow in my class?

4条回答
  •  悲哀的现实
    2021-02-05 14:42

    Imagine that -release is implemented in NSObject like this:

    - (void)release
    {
        _retainCount--;
        if (_retainCount == 0) {
            [self dealloc]
        }
    }
    

    I'm sure it's a little more complicated than that, but the answer to your question is that the object itself will call -dealloc when its retain count drops to zero. However, your custom class will inherit this behavior from NSObject. You'll never need to call -dealloc yourself in code that you write; it'll always happen automatically when the object has been properly released.

提交回复
热议问题