view raw
user3069232 user3069232 - 8 months ago 97
Swift Question

swift 2.0 to swift 3.0 NSNotification/Notification

Using XCode8 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] 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) {


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

class MyClass: NSObject {
    static let myNotification = Notification.Name("Blah")

    override init() {

        // Add observer:
                                               selector: #selector(notificationCallback),
                                               name: MyClass.myNotification,
                                               object: nil)

        // Post notification:
        let userInfo = ["foo": 1, "bar": "baz"] as [String: Any] MyClass.myNotification,
                                        object: nil,
                                        userInfo: userInfo)

    func notificationCallback(notification: Notification) {
        if let userInfo = notification.userInfo as NSDictionary? as? [String: Any] {

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)