【发布时间】: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