纯粹的官方代码使用NotificationCenter真的很难用,但是有了RxSwift,就变得方便了很多。
pod 'RxSwift'
pod 'RxCocoa'
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 优雅使用 NotificationCenter
原文:https://www.cnblogs.com/taoweiji/p/10915527.html