首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Web3.0中使用NotificationCenter传递数据,在Web2.0中如何使用NSNotificationCenter传递数据?

如何在Web3.0中使用NotificationCenter传递数据,在Web2.0中如何使用NSNotificationCenter传递数据?
EN

Stack Overflow用户
提问于 2016-04-28 09:47:03
回答 7查看 159.8K关注 0票数 160

我正在我的快速ios应用程序中实现socket.io

目前,在几个面板上,我正在监听服务器并等待传入的消息。为此,我在每个面板中调用getChatMessage函数:

代码语言:javascript
复制
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传递传入的消息。到目前为止,我能够传递发生了什么事情的信息,但没有传递数据本身。我是这样做的:

代码语言:javascript
复制
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil)

然后我有了一个叫做:

代码语言:javascript
复制
func showSpinningWheel(notification: NSNotification) {
}

每当我想叫它的时候,我都在做:

代码语言:javascript
复制
NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self)

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

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2016-04-28 09:55:41

SWIFT2.0

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

代码语言:javascript
复制
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   
    }
}

SWIFT3.0,4.0,5.0版本及以上

userInfo现在采用AnyHashable:有吗?作为一个论点,我们在Swift中作为字典文本提供

代码语言:javascript
复制
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。

代码语言:javascript
复制
extension Notification.Name {
static let myNotification = Notification.Name("myNotification")
}

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

Stack Overflow用户

发布于 2018-07-31 11:13:20

斯威夫特3

代码语言:javascript
复制
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
            }
        }
 }

斯威夫特4

代码语言:javascript
复制
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
 @objc 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
            }
        }
 }
票数 62
EN

Stack Overflow用户

发布于 2017-06-07 07:22:36

你好,萨希尔,我更新你的回答,为斯威夫特3

代码语言:javascript
复制
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
            }
        }
 }

希望能帮上忙。谢谢

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36910965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档