cómo pasar múltiples valores con una notificación en swift

Cómo enviar un número y una cadena a través de una notificación …

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

y recibir los valores en el receptor?

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

Puede envolverlos en un NSArray o un NSDictionary o un objeto personalizado.

P.ej:

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

Xcode 8.3.1 • Swift 3.1

 extension Notification.Name { static let refresh = Notification.Name("refresh") } 

 let myDict: [String: Any] = ["myInt": 1, "myText": "text"] NotificationCenter.default.post(name: .refresh, object: myDict) 

 NotificationCenter.default.addObserver(self, selector: #selector(refreshList), name: .refresh, object: nil) // don't forget vvv add an underscore before the view controller method parameter func refreshList(_ notification: Notification) { if let myDict = notification.object as? [String: Any] { if let myInt = myDict["myInt"] as? Int { print(myInt) } if let myText = myDict["myText"] as? String { print(myText) } } } 

Use el userInfo

 NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: ["number":yourNumber, "string":yourString] 

y para recuperar:

 func refreshList(notification: NSNotification){ let userInfo = notification.userInfo as Dictionary let receivednumber = userInfo["number"] let receivedString = userInfo["string"] } 

No soy fuerte en veloz (no probado), pero entiendes la idea.

En realidad, hay muchas formas de hacer esto. Uno de ellos es pasar una serie de objetos como:

 let arrayObject : [AnyObject] = [mynumber,mytext] NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: arrayObject) func refreshList(notification: NSNotification){ let arrayObject = notification.object as! [AnyObject] let receivednumber = arrayObject[0] as! Int let receivedString = arrayObject[1] as! String } 

Swift 4.0, estoy pasando una sola clave: valor, puede agregar varias claves y valores.

  NotificationCenter.default.post(name:NSNotification.Name(rawValue: "updateLocation"), object: ["location":"India"]) 

Agregar la definición de Observer y Método. También necesitas eliminar al observador.

 NotificationCenter.default.addObserver(self, selector: #selector(getDataUpdate), name: NSNotification.Name(rawValue: "updateLocation"), object: nil) @objc func getDataUpdate(notification: Notification) { guard let object = notification.object as? [String:Any] else { return } let location = object["location"] as? String self.btnCityName.setTitle(location, for: .normal) print(notification.description) print(notification.object ?? "") print(notification.userInfo ?? "") }