Using XCode 8 beta 6
under El Capitan coding in Swift 3.0
Trying to translate these lines in 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 cobble together 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 the notification when I run it and setup a listener with this line, but with no userInfo that I can decode?
let notificationName = Notification.Name("Blah")
NotificationCenter.default.addObserver(self, selector: #selector(peerChangedStateWithNotification), name: notificationName, object: nil)
This code prints "nil" as in no userInfo ...
func peerChangedStateWithNotification(notification:NSNotification) {
print("\(notification.userInfo)")
}
As @vadian said, NotificationCenter
has a
post(name:object:userInfo:)
method which you can use.
Here is a self-contained example, which also demonstrates how
to convert the userInfo
back to a dictionary of the expected type
(taken from https://forums.developer.apple.com/thread/61578):
class MyClass: NSObject {
static let myNotification = Notification.Name("Blah")
override init() {
super.init()
// Add observer:
NotificationCenter.default.addObserver(self,
selector: #selector(notificationCallback),
name: MyClass.myNotification,
object: nil)
// Post notification:
let userInfo = ["foo": 1, "bar": "baz"] as [String: Any]
NotificationCenter.default.post(name: MyClass.myNotification,
object: nil,
userInfo: userInfo)
}
func notificationCallback(notification: Notification) {
if let userInfo = notification.userInfo as? [String: Any] {
print(userInfo)
}
}
}
let obj = MyClass()
// ["bar": baz, "foo": 1]
Alternatively, you can extract the dictionary values in the callback like this (also from above Apple Developer Forum thread):
func notificationCallback(notification: Notification) {
guard let userInfo = notification.userInfo else { return }
if let fooValue = userInfo["foo"] as? Int {
print("foo =", fooValue)
}
if let barValue = userInfo["bar"] as? String {
print("bar =", barValue)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With