【发布时间】:2019-02-03 20:43:10
【问题描述】:
如何使用combineLatest 来返回BehaviorSubject?我正在尝试从 observable 中获取最后一个值。
var things: Observable<Thing>
// driven by UI
var selectedThingIndex: PublishSubject<Int>
// this is a BehaviorSubject, because I need to get the latest value outside a subscriber
var currentThing: BehaviorSubject<Thing> = BehaviorSubject.combineLatest(things, selectedThingIndex) { things, index in
things[index]
}
// Get the last value
let thing = currentThing.value()
我无法编译它,因为combineLatest 返回一个Observable,它似乎不能转换为BehaviorSubject。我尝试了一个明确的演员,例如as? BehaviorSubject<Thing?> 但返回 nil。
【问题讨论】:
-
注意...
Observables 和Subjects 应该是lets 而不是vars。
标签: rx-swift