【问题标题】:Swift Notification is sent but not receivedSwift 通知已发送但未收到
【发布时间】:2020-06-07 11:42:23
【问题描述】:

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

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】:

    真的是不匹配的问题

    • 您发送了此通知NSNotification.Name(rawValue: "test123")
    • 但预计会得到这个:NSNotification.Name(rawValue: "test")

    只是命名问题!

    【讨论】:

    • 好的,这是出乎意料和尴尬的:D 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多