2

也许标题没有提供很好的描述,所以请阅读以下内容。

我有一个设置为监听事件的通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.checkIfNotificationsWereTurnedOnAfterAlertShowing), name: UIApplicationWillEnterForegroundNotification, object: UIApplication.sharedApplication())

然后我想删除这个通知的观察者。我发现我需要像这样使用 deinit:

deinit  { 
    NSNotificationCenter.defaultCenter().removeObserver(UIApplicationWillEnterForegroundNotification)
    print("deinit")
}

但问题是当我关闭视图控制器时,程序从不执行 deinit 函数。在这个答案中,我发现这可能是由于强参考。

我检查了许多链接,但找不到如何为通知声明弱引用。那么如何声明一个弱通知呢?

希望我的问题很清楚。

期待您的帮助。

4

1 回答 1

2

我无法找到处理此问题的方法,deinit因此我决定删除viewWillDisappear对我来说效果很好的观察者。Rob Napier在上面的评论中建议

于 2016-08-08T15:03:17.137 回答