Swift NotificationCenter remove observer quickest way

后端 未结 3 1338
陌清茗
陌清茗 2021-02-09 23:31

I am adding a number of observers in my viewController -- applicationWillResignActive, applicationDidEnterBackground, and many others. I w

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 23:57

    @Sh_Khan is right:

    NotificationCenter.default.removeObserver(self)
    

    You can get even further, as mentioned in the 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.

提交回复
热议问题