问题描述
我有一个种子值 Triangle (Bool)
和一个发出值 Circle (Int)
的 observable.我的目的是在每次从该 observable 发出一个值时生成一个新的 observable (Triangle, Circle)
,将 Triangle
值转换为否定当前值.这是我的大理石图:
I have a seed value Triangle (Bool)
and an observable that emits values Circle (Int)
. My intention is to generate a new observable (Triangle, Circle)
each time a value is emitted from that observable, transforming the Triangle
value negating the current value. This is my marble diagram:
但是我无法实现它,我不知道scan
是否是正确的操作符.这是我的代码:
But I cannot achieve it, and I don't know if scan
is the correct operator. This is my code:
typealias Triangle = Bool
typealias Circle = Int
func scan() {
let triangle: Triangle = false
circleObservable
.scan(triangle, accumulator: ({ (triangle, circle) -> (Triangle, Circle) in
return (!triangle, circle)
}))
}
这会产生编译器错误:
无法转换类型 '(Triangle, Circle) -> (Triangle,Circle)'(又名 '(Bool, Int) -> (Bool, Int)')到预期的参数类型'(_, _) -> _'
谢谢!
推荐答案
circleObservable.scan((Triangle(false), Circle()), accumulator: { lastValue, circle -> (Triangle, Circle) in (!lastValue.0, circle) })
或者更简单:
typealias Triangle = Bool
typealias Circle = Int
func scan(circle: Observable<Circle>) -> Observable<(Triangle, Circle)> {
return circle.scan((Triangle(false), Circle())) { (!$0.0, $1) }
}
这篇关于Rx scan(),无法从种子和另一个可观察对象生成可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!