本章节将继续为大家介绍Rx 的方法使用。
map
将变量A转换为变量B
let originalSequence = Observable.of(Character("A"),Character("B"),Character("C"))
_ = originalSequence.map{ char in
return char.hashValue
}.subscribeNext({ (vc) -> Void in
print(vc);
})
}
/*
4799450059485597671
4799450059485597672
4799450059485597677
使用subscribe的输出结果:
**Next(4799450059485597671)**
**Next(4799450059485597672)**
**Next(4799450059485597677)**
**Completed**
*/
flatMap
transform the items emitted by an Observable into Observables, then flatten the emissions from those into a single Observable
** The FlatMap operator transforms an Observable by applying a function that you specify to each item emitted by the source Observable, where that function returns an Observable that itself emits items. FlatMap then merges the emissions of these resulting Observables, emitting these merged results as its own sequence.
This method is useful, for example, when you have an Observable that emits a series of items that themselves have Observable members or are in other ways transformable into Observables, so that you can create a new Observable that emits the complete collection of items emitted by the sub-Observables of these items.
Note that FlatMap merges the emissions of these Observables, so that they may interleave.
In several of the language-specific implementations there is also an operator that does not interleave the emissions from the transformed Observables, but instead emits these emissions in strict order, often called ConcatMap or something similar.*
将sub-Observables 的结果合并到flatmap的sequence 中
func flattenMap(){
let sequenceInt = Observable.of(1, 2, 3)
let sequenceString = Observable.of("A", "B", "C", "D", "E", "F", "--")
_ = sequenceInt
.flatMap { (x:Int) -> Observable in
print("from sequenceInt \(x)")
return sequenceString
}
.subscribe {
print($0)
}
/*
from sequenceInt 1
Next(A)
Next(B)
Next(C)
Next(D)
Next(E)
Next(F)
Next(--)
from sequenceInt 2
Next(A)
Next(B)
Next(C)
Next(D)
Next(E)
Next(F)
Next(--)
from sequenceInt 3
Next(A)
Next(B)
Next(C)
Next(D)
Next(E)
Next(F)
Next(--)
Completed
*/
}
scan
给observable的每一个item 运用一个function ,并返回结果
scan(0)初始值 和一个accumulator
- parameter seed: The initial accumulator value.
- parameter accumulator: An accumulator function to be invoked on each element.
- returns: An observable sequence containing the accumulated values.
*/
@warn_unused_result(message="http://git.io/rxs.uo")
public func scan(seed: A, accumulator: (A, Self.E) throws -> A) -> RxSwift.Observable
func scan(){
//
let sequenceToSum = Observable.of(0, 1, 2, 3, 4, 5)
_ = sequenceToSum
.scan(0) { acum, elem in
acum + elem
}
.subscribe {
print($0)
}
}
/*
Next(0)
Next(1)
Next(3)
Next(6)
Next(10)
Next(15)
Completed
*/
distinctUntilChanged
控制连续重复的item,如果连续的item相同,则不输出
let subscription = Observable.of(1, 2, 3, 1, 1, 4)
.distinctUntilChanged()
.subscribe {
print($0)
}
take
只取前n 项
let subscription = Observable.of(1, 2, 3, 4, 5, 6)
.take(3)
.subscribe {
print($0)
}
startWith
在输出序列之前,在序列前添加指定的序列项
// 在序列前依次添加序列项 0 ,1,2,3
func startWith(){
let _ = Observable.of(4, 5, 6, 7, 8, 9)
.startWith(3)
.startWith(2)
.startWith(1)
.startWith(0)
.subscribe {
print($0)
}
}
/*
**Next(0)**
**Next(1)**
**Next(2)**
**Next(3)**
新添加的项
**Next(4)**
**Next(5)**
**Next(6)**
**Next(7)**
**Next(8)**
**Next(9)**
**Completed**
*/
combinlatest
func combinlasted(){
let intOb1 = PublishSubject()
let intOb2 = PublishSubject()
_ = Observable.combineLatest(intOb1, intOb2) {
"\($0) \($1)"
}
.subscribe {
print($0)
}
intOb1.on(.Next("A"))
// intOb2.on(.Next(1))
intOb1.on(.Next("B"))
intOb2.on(.Next(2))
/*
Next(A 1)
Next(B 1)
Next(B 2)
// 注销 intOb2.on(.Next(1))
Next(B 2)
*/
}
另一版本
func combinLastedVer2(){
let intOb1 = Observable.just(2)
let intOb2 = Observable.of(0, 1, 2, 3, 4)
_ = Observable.combineLatest(intOb1, intOb2) {
"\($0) \($1)"
}
.subscribe {
print($0)
/*
Next(2 0)
Next(2 1)
Next(2 2)
Next(2 3)
Next(2 4)
Completed
*/
}
}
func combinLastedVer3(){
let intOb1 = Observable.just(2)
let intOb2 = Observable.of(0, 1, 2, 3)
let intOb3 = Observable.of(0, 1, 2, 3, 4)
_ = Observable.combineLatest(intOb1, intOb2, intOb3) {
"\($0) \($1) \($2)"
}
.subscribe {
print($0)
/*
Next(2 3 0)
Next(2 3 1)
Next(2 3 2)
Next(2 3 3)
Next(2 3 4)
Completed
*/
}
}
zip
将多个observable 的结果组合成一个并返回,但是必须每一个observable的item 要对应
func zip1(){
let intOb1 = PublishSubject()
let intOb2 = PublishSubject()
_ = Observable.zip(intOb1, intOb2) {
"\($0) \($1)"
}
.subscribe {
print($0)
}
intOb1.on(.Next("A"))
intOb2.on(.Next(1))
intOb1.on(.Next("B"))
intOb1.on(.Next("C"))
intOb2.on(.Next(2))
/*
Next(A 1)
Next(B 2)
*/
}
func zip2(){
let intOb1 = Observable.just(2)
let intOb2 = Observable.of(0, 1, 2, 3, 4)
_ = Observable.zip(intOb1, intOb2) {
"\($0) \($1)"
}
.subscribe {
print($0)
/*
Next(2 0)
Completed
*/
}
}
func zip3(){
let intOb1 = Observable.of(0, 1)
let intOb2 = Observable.of(0, 1, 2, 3)
let intOb3 = Observable.of(0, 1, 2, 3, 4)
_ = Observable.zip(intOb1, intOb2, intOb3) {
"\($0) \($1) \($2)"
}
.subscribe {
print($0)
}
/*
Next(0 0 0)
Next(1 1 1)
Completed
*/
}
merge
将Observable的item合并为一个
let subject1 = PublishSubject()
let subject2 = PublishSubject()
_ = Observable.of(subject1, subject2)
.merge()
.subscribeNext { int in
print(int)
}
subject1.on(.Next(20))
subject1.on(.Next(40))
subject1.on(.Next(60))
subject2.on(.Next(1))
subject1.on(.Next(80))
subject1.on(.Next(100))
subject2.on(.Next(1))
func merge2(){
let subject1 = PublishSubject()
let subject2 = PublishSubject()
_ = Observable.of(subject1, subject2)
.merge(maxConcurrent: 1)// 如果2 则结果与上述相同
.subscribe {
print($0)
}
subject1.on(.Next(20))
subject1.on(.Next(40))
subject1.on(.Next(60))
subject2.on(.Next(1))
subject1.on(.Next(80))
subject1.on(.Next(100))
subject2.on(.Next(1))
/*
Next(20)
Next(40)
Next(60)
Next(80)
Next(100)
*/
}
switchlatest
Switch subscribes to an Observable that emits Observables. Each time it observes one of these emitted Observables, the Observable returned by Switch unsubscribes from the previously-emitted Observable begins emitting items from the latest Observable. Note that it will unsubscribe from the previously-emitted Observable when a new Observable is emitted from the source Observable, not when the new Observable emits an item. This means that items emitted by the previous Observable between the time the subsequent Observable is emitted and the time that subsequent Observable itself begins emitting items will be dropped (as with the yellow circle in the diagram above)
func switchLatest(){
let var1 = Variable(0)
let var2 = Variable(200)
// var3 is like an Observable>
let var3 = Variable(var1.asObservable())
let d = var3
.asObservable()
.switchLatest()
.subscribe {
print($0)
}
var1.value = 1
var1.value = 2
var1.value = 3
var1.value = 4
var3.value = var2.asObservable()
var2.value = 201
var1.value = 5
var1.value = 6
var1.value = 7
/*
Next(0)
Next(1)
Next(2)
Next(3)
Next(4)
Next(200)
Next(201)
Completed
*/
}