首页 文章

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

提问于
浏览
82

我正在我的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 并将其包含在被调用的函数中?

4 回答

  • 213

    Swift 2.0

    使用 userInfo 传递信息,这是[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 version

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

    NOTE: 通知“names”不再是字符串,但是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)
    
  • 1

    您好@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
                }
            }
     }
    

    希望它有用 . 谢谢

  • 14

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

    对于Swift 4

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

    let dictionary = self.convertStringToDictionary(responceString)
    NotificationCenter.default.post(名称:NSNotification.Name(rawValue:"SOCKET_UPDATE"),对象:字典)

相关问题