Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2 to Swift 3 NSNotification/Notification

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)")
}
like image 617
user3069232 Avatar asked Sep 06 '16 14:09

user3069232


1 Answers

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)
        }
    }
like image 114
Martin R Avatar answered Oct 12 '22 09:10

Martin R