已发送但未收到快速通知

问题描述 投票:0回答:1

我添加了一个观察者,以便在连接充电器时得到通知。

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...")
}

我的概念中是否存在特定问题?

swift xcode cocoa nsnotificationcenter notificationcenter
1个回答
1
投票

您发送了此通知NSNotification.Name(rawValue: "test123")

    但希望得到这个:NSNotification.Name(rawValue: "test")
  • 这只是命名问题!
  • © www.soinside.com 2019 - 2024. All rights reserved.