Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift How to use combineLatest?

Tags:

I have defined:

let currentHours:Variable<Float> = Variable(0.0) let currentRate:Variable<Float>  = Variable(0.0) 

and I would like to make an Observable with combineLatest to sum these two value:

Observable.combineLatest(currentHours, currentRate, { (h, r) -> Float in     return Float(h+r) }) 

and I also try:

let c =  Observable.combineLatest(currentHours, currentRate) { $0 + $1 } 

I always get compiler error. thanks

like image 327
chiarotto.alessandro Avatar asked Feb 17 '16 22:02

chiarotto.alessandro


1 Answers

Try this:

let currentHours:Variable<Float> = Variable(0.0) let currentRate:Variable<Float>  = Variable(0.0)  let hoursAndRate = Observable.combineLatest(currentHours.asObservable(), currentRate.asObservable()){         return $0 + $1 } 

As you can see the key is in passing currentHours and currentRate as Observables in the function parameters.

like image 178
Erken Avatar answered Oct 22 '22 13:10

Erken