怎样才能
combineLatest
用于返回
BehaviorSubject
?我正试图从观测数据中得到最后一个值。
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()
我不能编译这个,因为
组合最
返回一个
Observable
,这似乎不适用于
行为主体
. 我尝试了一个明确的演员阵容,例如
as? BehaviorSubject<Thing?>
但结果是零。