Using Xcode8 beta 6 when encoding El Capitan in Swift 3.0
Trying to translate these lines into a project from Swift 2.0 to Swift 3.0
let userInfo = ["peer": peerID, "state": state.toRaw()]
NSNotificationCenter.defaultCenter.postNotificationName("Blah", object: nil, userInfo: userInfo)
So, I managed to combine this ...
public class MyClass {
static let myNotification = Notification.Name("Blah")
}
let userInfo = ["peerID":peerID,"state":state.rawValue] as [String : Any]
NotificationCenter.default.post(name: MyClass.myNotification, object: userInfo)
It compiles and sends a notification when I run it and configure the listener with this line, but without userInfo, which can I decode?
let notificationName = Notification.Name("Blah")
NotificationCenter.default.addObserver(self, selector: #selector(peerChangedStateWithNotification), name: notificationName, object: nil)
This code prints "nil", as without userInfo ...
func peerChangedStateWithNotification(notification:NSNotification) {
print("\(notification.userInfo)")
}
source
share