toArray : 是把 observable 结束时所有的事件值变成一个数组,只要这个 observable 不结束,这个转变就不会发生。
let bag = DisposeBag()
let number = PublishSubject<Int>()
number.asObserver().toArray().subscribe { (arr) in
print("toArray \(arr)")
}.disposed(by: bag)
number.onNext(1)
number.onNext(2)
number.onNext(3)
number.onCompleted()
scan :它可以对observable序列中的每一个事件进行 “累加”, 最终订阅到的 “累加” 的结果。
let bag = DisposeBag()
let number = PublishSubject<Int>()
number.scan(0) { (old, new) -> Int in
return old + new
}.subscribe { (num) in
print("scan \(num)")
}.disposed(by: bag)
number.onNext(1)
number.onNext(2)
number.onNext(3)
number.onCompleted()
note
map :自定义事件变化行为
let bag = DisposeBag()
let number = PublishSubject<Int>()
number.map { (value) -> Int in
return value * value
}.subscribe { (num) in
print("map \(num)")
}.disposed(by: bag)
number.onNext(1)
number.onNext(2)
number.onNext(3)
number.onCompleted()
note
网友评论