如何使用 swift 3.0 中的 NotificationCenter 和 swift 2.0 中的 NSNotificationCenter 传递数据? [英] How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

查看:24
本文介绍了如何使用 swift 3.0 中的 NotificationCenter 和 swift 2.0 中的 NSNotificationCenter 传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 swift ios 应用中实现 socket.io.

I'm implementing socket.io in my swift ios app.

目前在几个面板上,我正在侦听服务器并等待传入​​消息.我是通过在每个面板中调用 getChatMessage 函数来实现的:

Currently on several panels I'm listening to the server and wait for incoming messages. I'm doing so by calling the getChatMessage function in each panel:

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

但是我注意到这是一个错误的方法,我需要改变它 - 现在我只想开始监听传入的消息一次,当任何消息到来时 - 将此消息传递给任何监听它的面板.

However I noticed it's a wrong approach and I need to change it - now I want to start listening for incoming messages only once and when any message comes - pass this message to any panel that listens to it.

所以我想通过 NSNotificationCenter 传递传入的消息.到目前为止,我能够传递发生了某些事情的信息,但不能传递数据本身.我是这样做的:

So I want to pass the incoming message through the NSNotificationCenter. So far I was able to pass the information that something happened, but not pass the data itself. I was doing that by:

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

然后我有一个函数叫做:

then I had a function called:

func showSpinningWheel(notification: NSNotification) {
}

任何时候我都想称之为我正在做的:

and any time I wanted to call it I was doing:

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

那么如何传递对象 messageInfo 并将其包含在被调用的函数中?

So how can I pass the object messageInfo and include it in the function that gets called?

推荐答案

Swift 2.0

使用 userInfo 传递信息,userInfo 是 [NSObject : AnyObject] 类型的可选字典?

Pass info using userInfo which is a optional Dictionary of type [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、4.0、5.0 及以上版本

userInfo 现在采用 [AnyHashable: Any]?作为参数,我们在 Swift 中将其作为字典文字提供

The userInfo now takes [AnyHashable: Any]? as an argument, which we provide as a dictionary literal in 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
 // For swift 4.0 and above put @objc attribute in front of function Definition  
 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.

NOTE: Notification "names" are no longer strings, but are of type Notification.Name, hence why we are using NSNotification.Name(rawValue: "notificationName") and we can extend Notification.Name with our own custom notifications.

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

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

这篇关于如何使用 swift 3.0 中的 NotificationCenter 和 swift 2.0 中的 NSNotificationCenter 传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆