2015-05-19 12 views
14

Come inviare un numero e una stringa attraverso una notifica ...come passare più valori con una notifica in rapida

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

e ricevere i valori nel ricevitore?

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

risposta

22

li Si potrebbe avvolgere in un NSArray o un NSDictionary o di un oggetto personalizzato.

Esempio:

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

Grazie funziona, ma ottengo il valore "opzionale (MyText)" per il testo. È questo dovrebbe essere e devo rimuovere la stringa opzionale o sto facendo qualcosa di sbagliato. –

+0

@mcflysoft Dai un'occhiata a questo su Optionals: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5 -ID330 – Moritz

11

Utilizzare il userInfo

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

e per recuperare:

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

Im non forte sul veloce (non testato), ma si ottiene l'idea.

3

In realtà ci sono molti modi per farlo. Uno di loro è di passare un array di oggetti come:

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

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

Grazie per il link! – MeV

0

Swift 4.0, sto passando una singola chiave: valore, è possibile aggiungere più chiavi e valori.

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

Aggiunta Observer e Metodo definizione

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 ?? "") 
    } 
Problemi correlati