RxSwift How to use combineLatest?

chiarotto.alessandro picture chiarotto.alessandro · Feb 17, 2016 · Viewed 20.9k times · Source

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

Answer

Erken picture Erken · Feb 22, 2016

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.