美文网首页Swift
通过 RxSwift 优雅使用 NotificationCent

通过 RxSwift 优雅使用 NotificationCent

作者: ImWiki | 来源:发表于2019-05-23 23:48 被阅读0次

纯粹的官方代码使用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])

相关文章

网友评论

    本文标题:通过 RxSwift 优雅使用 NotificationCent

    本文链接:https://www.haomeiwen.com/subject/heaizqtx.html