RxSwift如何使用combineLatest?

时间:2016-02-17 22:30:43

标签: ios swift reactive-programming observable

我定义了:

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

我想用combineLatest制作一个Observable来对这两个值求和:

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

我也尝试:

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

我总是遇到编译器错误。 感谢

1 个答案:

答案 0 :(得分:48)

试试这个:

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
}

正如您所看到的那样,关键是在函数参数中将currentHours和currentRate 作为Observables 传递。