是否需要ARC中的NSNotificationCenter removeObserver? [英] Is NSNotificationCenter removeObserver in ARC needed?

查看:147
本文介绍了是否需要ARC中的NSNotificationCenter removeObserver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加观察者是否会增加对象的保留计数?
如果是,ARC是否也会处理此观察者的移除?如果没有,我应该在哪里删除观察者?

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?

推荐答案

即使您使用,也应该明确删除观察者ARC 。创建一个 dealloc 方法并删除那里..

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

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

如果您看到该方法,则无需拨打 [super dealloc]; 这里,只需要没有超级dealloc的方法。

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

Swift的更新

如果要在swift中编写代码,可以在 deinit 方法中删除观察者。

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

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

这篇关于是否需要ARC中的NSNotificationCenter removeObserver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆