2017-11-14 3 views
0

新は私がSWIFT 3.0でNSNotificationCenterを言及したコードの行の下に知っていただきたいと思います

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 
} 
} 

答えて

2

私はあなたがこれを行う方法を求めていると仮定していRxSwif/RxCocoaに変換することができます。反応性ココア。 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。

+0

これは手作業で処理する必要があると思うので、 '.tser(duringLifetimeOf:self)'の前に '.observeValues'を追加します。 – jjoelson

+0

ええ、良い点は、私は答えを更新します – MeXx

関連する問題