gpt4 book ai didi

ios - Swift NotificationCenter 删除观察者最快的方法

转载 作者:行者123 更新时间:2023-11-28 17:58:59 30 4
gpt4 key购买 nike

我在我的 viewController 中添加了一些观察者 -- applicationWillResignActiveapplicationDidEnterBackground 以及许多其他观察者。我想在一行中删除 self 作为所有已注册通知的观察者。我的问题是以下行是否足以做到这一点,或者此代码是否存在问题?

deinit {
NotificationCenter.default.removeObserver(self)
}

最佳答案

@Sh_Khan 是对的:

NotificationCenter.default.removeObserver(self)

您可以更进一步,如 Apple Documentation 中所述:

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.

关于ios - Swift NotificationCenter 删除观察者最快的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51745194/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com