一尘不染

如何在通知中快速传递多个值

swift

如何通过通知发送数字和字符串…

let mynumber=1;
let mytext="mytext";
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????);

并在接收器中接收值?

func refreshList(notification: NSNotification){
        let receivednumber=??????????
        let receivedString=?????????
    }

阅读 264

收藏
2020-07-07

共1个答案

一尘不染

您可以将它们包装在NSArrayNSDictionary或自定义对象中。

例如:

let mynumber=1;
let mytext="mytext";

let myDict = [ "number": mynumber, "text":mytext]

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object:myDict);

func refreshList(notification: NSNotification){
    let dict = notification.object as! NSDictionary
    let receivednumber = dict["number"]
    let receivedString = dict["mytext"]
}
2020-07-07