【发布时间】:2015-08-28 03:08:35
【问题描述】:
我在视图控制器中有以下代码来注册我的自定义通知之一。到目前为止,我一直使用选择器进行注册,但我想我会尝试使用闭包,但发现有些奇怪。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationReceived:", name: "NotificationKey", object: nil)
NSNotificationCenter.defaultCenter().addObserverForName("NotificationKey", object: nil, queue: nil) { [weak self] notification in
NSLog("Notification received in closure!")
}
@objc private func notificationReceived(notification: NSNotification) {
NSLog("Notification received!")
}
然后我将视图控制器作为观察者移除。
NSNotificationCenter.defaultCenter().removeObserver(self)
一旦观察者被移除,我仍然会在 Closure 中看到 NSLog,但在选择器函数中看不到 NSLog。通知中心似乎正在关闭关闭。我还注意到,如果在其中引用 self ,则闭包会导致保留循环(添加 [weak self] 可以解决此问题,但仍会调用 NSLog 行)。
有谁知道为什么关闭仍在处理通知?
是否会出现在选择器上使用闭包的情况(我更喜欢它们,因为它避免了魔术字符串)?
【问题讨论】: