2016-04-28 16 views
55

Ich implementiere socket.io in meiner schnellen ios App.Wie Daten mit NotificationCentre in Swift 3.0 und NSNotificationCenter in Swift 2.0 übergeben?

Momentan höre ich auf mehreren Panels den Server und warte auf eingehende Nachrichten. Ich mache so durch die getChatMessage Funktion in jeder Platte Aufruf:

func getChatMessage(){ 
    SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in 
     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      //do sth depending on which panel user is 
     }) 
    } 
} 

Allerdings bemerkte ich, es ist ein falscher Ansatz, und ich brauche, es zu ändern - jetzt will ich für eingehende Nachrichten hören nur einmal starten und wenn jede Nachricht kommt - Übergeben Sie diese Nachricht an jedes Panel, das darauf hört.

Also ich möchte die eingehende Nachricht über das NSNotificationCenter übergeben. Bisher konnte ich die Information weitergeben, dass etwas passiert ist, aber die Daten selbst nicht weitergeben. Ich tat, dass durch:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil) 

dann hatte ich eine Funktion namens:

func showSpinningWheel(notification: NSNotification) { 
} 

und jedes Mal, wenn ich wollte es nenne ich tat:

NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self) 

So wie kann ich übergeben Sie das Objekt messageInfo und fügen Sie es in die Funktion, die aufgerufen wird?

+1

Verwendung Methode mit Userinfo ... 'NSNotificationCenter.defaultCenter() postNotificationName ("hideSpinner", Objekt: null, userinfo: yourvalue).' –

+0

hm ok, und wie kann ich holen diese 'yourValue' in der Funktion, die wird diese Benachrichtigung aufgerufen (in 'showSpinningWheel')? – user3766930

+0

using 'userinfo' wie 'notification.userinfo' –

Antwort

149

Swift 2,0

Pass info userInfo verwendet, die ein optionales Wörterbuch vom Typ [NSObject: ANYOBJECT]?

let imageDataDict:[String: UIImage] = ["image": image] 

    // Post a notification 
    NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict) 

// Register to receive notification in your class 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil) 

// handle notification 
func showSpinningWheel(notification: NSNotification) { 
    if let image = notification.userInfo?["image"] as? UIImage { 
    // do something with your image 
    } 
} 

Swift 3.0 Version

Die userinfo nimmt jetzt [AnyHashable: Alles]? als Argument, die wir als Wörterbuch in Swift wörtlichen bieten

let imageDataDict:[String: UIImage] = ["image": image] 

    // post a notification 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict) 
    // `default` is now a property, not a method call 

// Register to receive notification in your class 
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil) 

// handle notification 
func showSpinningWheel(_ notification: NSNotification) { 

    if let image = notification.userInfo?["image"] as? UIImage { 
    // do something with your image 
    } 
} 

HINWEIS: daher Mitteilung „Namen“ sind keine Saiten länger, aber vom Typ Notification.Name sind, warum sind wir NSNotification.Name(rawValue:"notificationName") mit und wir können Erweitern Sie Notification.Name mit unseren eigenen benutzerdefinierten Benachrichtigungen.

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

// and post notification like this 
NotificationCenter.default.post(name: .myNotification, object: nil) 
+0

Gosh ist es so kompliziert – MarksCode

+6

@MarksCode, es ist nicht, vertrau mir :) Sobald Sie es implementieren, werden Sie sehen, wie einfach dieser Code ist und lachen wird :)) – mimic

+0

Wie Es klappt? verwirrt. Nicht aufgelöste Kennung – HamasN

6

Hallo @sahil ich Ihre Antwort für eine schnelle 3

let imageDataDict:[String: UIImage] = ["image": image] 

    // post a notification 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict) 
    // `default` is now a property, not a method call 

// Register to receive notification in your class 
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil) 

// handle notification 
func showSpinningWheel(_ notification: NSNotification) { 
     print(notification.userInfo ?? "") 
     if let dict = notification.userInfo as NSDictionary? { 
      if let id = dict["image"] as? UIImage{ 
       // do something with your image 
      } 
     } 
} 

aktualisieren Hoffe, dass es hilfreich ist. Dank

+0

Ich habe das schon im Oktober 2016 getan. – Sahil

+3

sollte notification.userinfo sein, nicht notification.object –