Swift NotificationCenter删除观察者最快的方法 [英] Swift NotificationCenter remove observer quickest way
问题描述
我在viewController
-applicationWillResignActive
,applicationDidEnterBackground
和其他许多对象中添加了许多观察者.我想删除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屋!