how to pass multiple values with a notification in swift

前端 未结 6 1741
广开言路
广开言路 2020-11-27 05:06

How to send a number and a String through a notification ...

let mynumber=1;
let mytext=\"mytext\";
NSNotificationCenter.defaultCenter().postNotificationName         


        
6条回答
  •  無奈伤痛
    2020-11-27 05:33

    You could wrap them in an NSArray or a NSDictionary or a custom Object.

    Eg:

    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"]
    }
    

提交回复
热议问题