本文是示例首要来自于官方的实例代码,可从这儿下载 ,实例代码稍有改动。其中的图片来自于reactivex.io/
本文首要的作用是检查实例代码并检查对应实例代码的作用,不需要运转项目
操作符目录
-
RxSwift官方运用示例之<创建和订阅Observables>
- never、empty、just、of、from、create、range、repeatElement、generate、deferred、error、doOn
-
RxSwift官方运用示例之<运用Subjects>
- PublishSubject、ReplaySubject、BehaviorSubject
-
RxSwift官方运用示例之<组合操作符>
- startWith、merge、zip、combineLatest、Array.combineLatest、switchLatest、withLatestFrom
-
RxSwift官方运用示例之<转换操作符>
- map、flatMap 和 flatMapLatest、scan
-
RxSwift官方运用示例之<过滤及条件操作符>
- filter、distinctUntilChanged、elementAt、single、take、takeLast、takeWhile、takeUntil、skip、skipWhile、skipWhileWithIndex、skipUntil
-
RxSwift官方运用示例之<数学和聚合操作符>
- toArray、reduce、concat
-
RxSwift官方运用示例之<可衔接操作符>
- interval、publish、replay、multicast
-
RxSwift官方运用示例之<错误处理操作符>
- catchAndReturn、catchError、retry、retry(_:)
debug 操作符会打印一切的 subscriptions, events, disposals
filter
只宣布 Observable
序列中满意指定条件的元素
let disposeBag = DisposeBag()
Observable.of(2, 30, 22, 5, 60, 1)
.filter {
$0 > 10
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
30
22
60
distinctUntilChanged
抑制由 Observable
序列宣布的接连重复元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 2, 1, 3)
.distinctUntilChanged()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
1
2
1
3
elementAt
只发射 Observable
序列指定索引处的元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4)
.element(at: 2)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
3
single
仅发射 Observable
序列发射的第一个元素(或第一个满意条件的元素)。 假如 Observable
序列没有宣布恰好一个元素,则会抛犯错误
let disposeBag = DisposeBag()
Observable.of("", "", "", "", "", "")
.single()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
Unhandled error happened: Sequence contains more than one element.
此刻会有错误信息打印,假如想处理该错误信息,能够加上 onError:
,如下
let disposeBag = DisposeBag()
Observable.of("", "", "", "", "", "")
.single()
.subscribe(onNext: { print($0) }, onError: { print("犯错啦:", $0) } )
.disposed(by: disposeBag)
打印
犯错啦: Sequence contains more than one element.
此外,single
还能够加上条件判别
let disposeBag = DisposeBag()
Observable.of("", "", "", "", "", "")
.single { $0 == "" }
.subscribe { print($0) }
.disposed(by: disposeBag)
Observable.of("", "", "", "", "", "")
.single { $0 == "" }
.subscribe { print($0) }
.disposed(by: disposeBag)
Observable.of("", "", "", "", "", "")
.single { $0 == "" }
.subscribe { print($0) }
.disposed(by: disposeBag)
打印
next()
completed
next()
error(Sequence contains more than one element.)
error(Sequence doesn't contain any elements.)
从打印能够看出,第一个打印正常,输出一个结果之后当即completed;第二个打印反常,由于有2个,所以抛出反常 error(Sequence contains more than one element.);第三个也反常,由于序列中没有,抛出反常error(Sequence doesn’t contain any elements.)
take
仅从 Observable 序列的最初宣布指定数量的元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4)
.take(2)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
1
2
takeLast
仅从 Observable 序列的末尾宣布指定数量的元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4)
.take(1)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
4
takeWhile
从 Observable 序列的最初宣布满意条件的元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.take(while: { $0 < 4 } )
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
1
2
3
takeUntil
从源 Observable
序列中宣布元素,直到引用 Observable 序列宣布元素
let disposeBag = DisposeBag()
let sourceSequence = PublishSubject<String>()
let referenceSequence = PublishSubject<String>()
sourceSequence
.take(until: referenceSequence)
.subscribe { print($0) }
.disposed(by: disposeBag)
sourceSequence.onNext("1")
sourceSequence.onNext("2")
sourceSequence.onNext("3")
sourceSequence.onNext("3")
sourceSequence.onNext("4")
sourceSequence.onNext("5")
referenceSequence.onNext("0")
sourceSequence.onNext("6")
sourceSequence.onNext("7")
sourceSequence.onNext("8")
referenceSequence.onNext("0")
sourceSequence.onNext("9")
打印
next(1)
next(2)
next(3)
next(3)
next(4)
next(5)
completed
skip
从 Observable
序列的最初越过发射指定数量的元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4)
.skip(2)
.subscribe { print($0) }
.disposed(by: disposeBag)
打印
next(3)
next(4)
completed
skipWhile
从 Observable
序列的最初越过发射满意条件的元素
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.skip(while: { $0 < 4 } )
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
打印
4
5
6
skipWhileWithIndex
从 Observable
序列的最初越过发射满意条件的元素
这不便是 skipWhile 操作符吗,不过是带索引的skip
let disposeBag = DisposeBag()
Observable.of("", "", "", "", "", "")
.enumerated()
.skip(while: { $0.index < 3 } )
.subscribe(onNext: { print($0) } )
.disposed(by: disposeBag)
打印
(index: 3, element: "")
(index: 4, element: "")
(index: 5, element: "")
skipUntil
越过从源 Observable
序列中发射元素,直到引用 Observable
序列发射一个元素。
let disposeBag = DisposeBag()
let sourceSequence = PublishSubject<String>()
let referenceSequence = PublishSubject<String>()
sourceSequence
.skip(until: referenceSequence)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
sourceSequence.onNext("1")
sourceSequence.onNext("2")
sourceSequence.onNext("3")
sourceSequence.onNext("4")
sourceSequence.onNext("5")
referenceSequence.onNext("0")
sourceSequence.onNext("6")
sourceSequence.onNext("7")
sourceSequence.onNext("8")
referenceSequence.onNext("0")
sourceSequence.onNext("9")
打印
6
7
8
9