如何使用Swift 3.0中的NotificationCentre和Swift 2.0中的NSNotificationCenter传递数据?

问题描述:

我在我的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' –

夫特使用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

你好@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 –