原文swift
純粹的官方代碼使用NotificationCenter真的很難用,可是有了RxSwift,就變得方便了不少。post
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])