swift - Swift 通知已发送但未收到
问题描述
我添加了一个观察者,以便在连接充电器时收到通知。
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
nil,
{ (_, _, _, _, _) in
print("Sending...")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "test123"), object: nil)
},
kIOPSNotifyAttach as CFString?,
nil,
.deliverImmediately
)
连接充电器后,会发送通知,以便我可以在我的应用程序中做出反应。所以我在我的应用程序中添加了一个观察者
NotificationCenter.default.addObserver(
self,
selector: #selector(reactToNotification(_:)),
name: NSNotification.Name(rawValue: "test123"),
object: nil)
通知已正确发送,但永远不会调用关联的函数。
@objc func reactToNotification(_ notification: Notification) {
print("Receiving...")
}
我的概念中是否存在特定问题?
解决方案
真的是不匹配的问题
- 您发送了此通知
NSNotification.Name(rawValue: "test123")
- 但预计会得到这个:
NSNotification.Name(rawValue: "test")
只是命名问题!
推荐阅读
- php - 如何从 PHP curl 解码二进制响应
- javascript - 如果 Woocommerce 类别中的产品计数为零,则重定向自定义页面
- c# - C# 中的 RTSP ip 摄像头连接或 vlc 图像处理
- python - Django form.is_valid() 返回 False
- django - --View 处的 ValueError 未返回 HttpResponse 对象。它返回 None 而不是
- javascript - 在高图表堆叠条形图中的对象中存在空值的情况下,图表中的显示空间
- javascript - 如何在大表中定义列大小(368 列)
- azure - 在多个进程中使用来自 Azure 服务总线的消息
- spring - 如果未注入其组件类,则不会执行 Spring Scheduler
- javascript - 如何从终端运行 esnext 文件