我有一个协议定义:
protocol Environment {
var url: String {get}
}
我将它作为对象提供给NotificationCenter
:
NotificationCenter.default.post(name: NotificationForEnvironmentChange, object: Environment)
但它没有显示在我为NotificationForEnvironmentChange
选择器分配的函数的object属性中。
func environmentChange(sender: Notification) {
guard let newEnvironment = sender.object as? Environment else {
return
}
//Never gets the environment
}
为什么?
注意:NotificationForEnvironmentChange是我定义的Notification
属性,如下所示:
let NotificationForEnvironmentChange = Notification.Name("NotificationForEnvironmentChange")