ios - react 迅速的新版本NSNotificationCenter

标签 ios iphone reactive-cocoa 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 = $0.userInfo?["image"] as? UIImage {
      // do something with your image
    }
}


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

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

关于ios - react 迅速的新版本NSNotificationCenter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47285169/

相关文章:

iphone - UISegmentedControl 和添加目标

c# - 如何使用 Monotouch for iPad 创建圆形按钮?

android - 如何检测 map 何时完成调整大小

ios - 当应用程序处于后台或运行状态时而不是应用程序终止时处理推送通知

ios - 如何关闭通知内容扩展?

iphone - 从照片库中选择多张图片

iphone - 录制iPhone应用程序视频(无需模拟器)

ios - 使用观察者发出信号量?

ios - 使用 ReactiveCocoa 观察 NSArray 中的对象变化

ios - 将 RACCommand 组合成一个共同结果的最佳方法是什么?