Swift 2 到 Swift 3 NSNotification/通知 [英] Swift 2 to Swift 3 NSNotification/Notification

查看:60
本文介绍了Swift 2 到 Swift 3 NSNotification/通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 3.0

尝试将项目中的这些行从 Swift 2.0 翻译成 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)

当我运行它并使用此行设置侦听器时,它会编译并发送通知,但没有我可以解码的 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)

此代码打印nil",因为没有 userInfo ...

This code prints "nil" as in no userInfo ...

func peerChangedStateWithNotification(notification:NSNotification) {
    print("\(notification.userInfo)")
}

推荐答案

正如@vadian 所说,NotificationCenter 有一个post(name:object:userInfo:) 你可以使用的方法.

As @vadian said, NotificationCenter has a post(name:object:userInfo:) method which you can use.

这是一个自包含的示例,它也演示了如何将 userInfo 转换回预期类型的​​字典(摘自 https://forums.developer.apple.com/thread/61578):

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)
        }
    }

这篇关于Swift 2 到 Swift 3 NSNotification/通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆