2016-04-28 94 views
55

我在我的swift ios應用中實現了socket.io如何使用Swift 3.0中的NotificationCentre和Swift 2.0中的NSNotificationCenter傳遞數據?

目前在幾個面板上我正在監聽服務器並等待收到的消息。我通過調用每個面板的getChatMessage功能這樣做:

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

但是我注意到這是一個錯誤的做法,我需要去改變它 - 現在我要開始監聽傳入的消息只有一次,當任何消息來 - 將此消息傳遞給任何偵聽它的面板。

所以我想通過NSNotificationCenter傳入消息。到目前爲止,我能夠傳遞發生了什麼事情的信息,但不傳遞數據本身。我是這樣做的:

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

然後我有一個調用的函數:

func showSpinningWheel(notification: NSNotification) { 
} 

,我想叫它我做的任何時間:

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

所以,我怎麼能傳遞對象messageInfo並將其包含在被調用的函數中?

+1

用戶信息的使用方法...'NSNotificationCenter.defaultCenter()。postNotificationName(「hideSpinner」,object:nil,userInfo:yourvalue)' –

+0

hm好的,我該如何在函數中獲取這個'yourValue'被通知調用(在'showSpinningWheel'中)? – user3766930

+0

使用'.userinfo'就像'notification.userinfo' –

回答

149

夫特使用userInfo它的類型的一個可選的字典2.0

通行證信息[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 
    } 
} 

雨燕3.0的版本

用戶信息現在只需[AnyHashable:任何]?作爲參數,由我們提供的字典字面斯威夫特

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

注:通知「名稱」不再是字符串,但類型Notification.Name的,因此爲什麼我們使用NSNotification.Name(rawValue:"notificationName"),我們可以使用我們自己的自定義通知來擴展Notification.Name。

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

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

天哪這是如此複雜 – MarksCode

+6

@MarksCode,它不是,相信我:)只要你實現它,你會看到這個代碼是多麼簡單,並會笑:)) – mimic

+0

如何有用?困惑。未解決的標識符。 – HamasN

6

你好@sahil我更新了SWIFT 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 
      } 
     } 
} 

答案希望這是有幫助的。謝謝

+0

我已經在2016年10月完成了這項工作。 – Sahil

+3

應該是notification.userinfo,not notification.object –