也许标题没有提供很好的描述,所以请阅读以下内容。
我有一个设置为监听事件的通知:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.checkIfNotificationsWereTurnedOnAfterAlertShowing), name: UIApplicationWillEnterForegroundNotification, object: UIApplication.sharedApplication())
然后我想删除这个通知的观察者。我发现我需要像这样使用 deinit:
deinit {
NSNotificationCenter.defaultCenter().removeObserver(UIApplicationWillEnterForegroundNotification)
print("deinit")
}
但问题是当我关闭视图控制器时,程序从不执行 deinit 函数。在这个答案中,我发现这可能是由于强参考。
我检查了许多链接,但找不到如何为通知声明弱引用。那么如何声明一个弱通知呢?
希望我的问题很清楚。
期待您的帮助。