如何使用Swift 3.0中的NotificationCentre和Swift 2.0中的NSNotificationCenter传递数据?
我在我的swift ios应用程序中实现socket.io
。
目前在几个面板上我正在监听服务器,并等待传入的消息。 我通过调用每个面板中的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
并将其包含在被调用的函数中呢?
Swift 2.0
使用userInfo
这是一个可选的types[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版本
userInfo现在需要[AnyHashable:Any]? 作为一个参数,我们在Swift中提供了一个字典文字
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 } }
注意:通知“名称”不再是string,而是Notification.Nametypes,因此我们为什么要使用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)
你好@sahil我更新你的答案迅速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.object ?? "") if let dict = notification.object as? NSDictionary { if let id = dict["image"] as? UIImage{ // do something with your image } } }
希望这是有帮助的。 谢谢