原文

纯粹的官方代码使用NotificationCenter真的很难用,但是有了RxSwift,就变得方便了很多。

修改 Podfile,通过pod引入RxSwift
  pod 'RxSwift'
  pod 'RxCocoa'
通过 RxSwift 注册监听器
import RxSwift
import RxCocoa

let notificationName = Notification.Name("UploadStatus")
NotificationCenter.default.rx.notification(notificationName).subscribe(onNext: { notification in
    if(notification.object != nil){
        print("上传状态:\(notification.object!)")
    }
    if(notification.userInfo != nil){
        print("参数:\(notification.userInfo!)")
    }
})
发送通知
let notificationName = Notification.Name("UploadStatus")
NotificationCenter.default.post(name: notificationName, object: "上传失败")

NotificationCenter.default.post(name: notificationName, object: nil, userInfo: ["param1":"Wiki","param2":18])

相关文章:

  • 2021-12-15
  • 2021-06-18
  • 2021-11-06
  • 2021-11-12
  • 2021-11-03
  • 2022-02-18
  • 2022-01-10
  • 2022-12-23
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2021-12-13
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案