Reputation: 571
I'm trying to pass userInfo from an object to an observing view controller. For some reason it keeps crashing. Here's the code:
Observer:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.alarmFired(_:)),
name: Notification.Name(rawValue: "AlarmFiringNotification"),
object: nil)
Receiving function:
@objc func alarmFired(_ notification: UNNotification) {
let userInfo = notification.request.content.userInfo
let title = userInfo["title"] as? String
let body = userInfo["body"] as? String
let alert = UIAlertController(title: title, message: body, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okAction)
}
Notification broadcasting:
public static func broadcastAlarmFiringNotification(with userInfo: [String: Any]) {
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "AlarmFiringNotification"), object: self, userInfo: userInfo))
}
Broadcasting call:
NotificationBroadcaster.broadcastAlarmFiringNotification(with: ["title": title, "body": body])
But it keeps crashing with this output:
2018-03-14 18:51:06.743621-0400 App[33718:871608] -[NSConcreteNotification request]: unrecognized selector sent to instance 0x60c00005c860
2018-03-14 18:51:06.754868-0400 App[33718:871608] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteNotification request]: unrecognized selector sent to instance 0x60c00005c860'
It crashes on the notification broadcasting (NotificationCenter.default.post...)
I'm really confused, any help is appreciated!
Upvotes: 0
Views: 804
Reputation: 22701
The UNNotification
class is not what you want here. Change the signature to:
@objc func alarmFired(notification: Notification) {
let userInfo = notification.userInfo
Upvotes: 1