Swift NotificationCenter删除观察者最快的方法 [英] Swift NotificationCenter remove observer quickest way

查看:457
本文介绍了Swift NotificationCenter删除观察者最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在viewController-applicationWillResignActiveapplicationDidEnterBackground和其他许多对象中添加了许多观察者.我想删除self作为一行中所有已注册通知的观察者.我的问题是,下面的代码行是否足以完成此操作,或者此代码是否存在问题?

I am adding a number of observers in my viewController -- applicationWillResignActive, applicationDidEnterBackground, and many others. I want to remove self as observer to all registered notifications in one line. My question is whether the following line is enough to do that, or are there issues with this code?

deinit {
   NotificationCenter.default.removeObserver(self)
}

推荐答案

@Sh_Khan是正确的:

@Sh_Khan is right:

NotificationCenter.default.removeObserver(self)

Apple文档:

如果您的应用程序针对iOS 9.0及更高版本或macOS 10.11及更高版本,则无需在其dealloc方法中注销观察者.

If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to unregister an observer in its dealloc method.

这篇关于Swift NotificationCenter删除观察者最快的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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