反应式 swift 中的新版本控制 NSNotificationCenter

New versioning NSNotificationCenter in reactive swift

我想知道下面我在swift 3.0中提到的NSNotificationCenter代码行可以转换成RxSwif/RxCocoa

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

我假设你问的是如何在 ReactiveCocoa 中做到这一点。 在 ReactiveCocoa 中,所有扩展都可以通过 .reactive 成员获得:

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


NotificationCenter.default.reactive.notifications(forName: .myNotification)
  .take(duringLifetimeOf: self)
  .observeValues {
    if let image = [=10=].userInfo?["image"] as? UIImage {
      // do something with your image
    }
}


NotificationCenter.default.post(name: .myNotification, object: nil, userInfo: ["image": image])

编辑:感谢@jjoelson 提到观察结果的处置。