Is NSNotificationCenter removeObserver in ARC needed? [duplicate]

独自空忆成欢 提交于 2019-12-29 03:17:06

问题


Does adding an observer increase the retain count of an object? If yes, does ARC handle the removing of this observer too? If not, where should I remove the observer?


回答1:


You should explicitly remove the observer even you use ARC. Create a dealloc method and remove there..

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

If you see the method you don't need to call [super dealloc]; here, only the method without super dealloc needed.

UPDATE for Swift

You can remove observer in deinit method if you are writing code in swift.

deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
       }


来源:https://stackoverflow.com/questions/15656367/is-nsnotificationcenter-removeobserver-in-arc-needed

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