RxSwift官方使用示例之<過濾及條件操作符>

語言: CN / TW / HK

本文是示例主要來自於官方的例項程式碼,可從這裡下載 ,例項程式碼稍有改動。其中的圖片來自於reactivex.io/

本文主要的作用是檢視例項程式碼並檢視對應例項程式碼的效果,不需要執行專案

操作符目錄

debug 操作符會列印所有的 subscriptions, events, disposals

filter

只發出 Observable 序列中滿足指定條件的元素

image.png

``` 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 序列指定索引處的元素

image.png

``` 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 序列的開頭髮出指定數量的元素

image.png ``` let disposeBag = DisposeBag()

Observable.of(1, 2, 3, 4) .take(2) .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) ``` 列印

1 2

takeLast

僅從 Observable 序列的末尾發出指定數量的元素

image.png

``` let disposeBag = DisposeBag()

Observable.of(1, 2, 3, 4) .take(1) .subscribe(onNext: { print($0) }) .disposed(by: disposeBag) ``` 列印

4

takeWhile

從 Observable 序列的開頭髮出滿足條件的元素 image.png ``` 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 序列發出元素

image.png

``` let disposeBag = DisposeBag()

let sourceSequence = PublishSubject() let referenceSequence = PublishSubject()

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 序列的開頭跳過發射指定數量的元素 image.png

``` let disposeBag = DisposeBag()

Observable.of(1, 2, 3, 4) .skip(2) .subscribe { print($0) } .disposed(by: disposeBag) ``` 列印

next(3) next(4) completed

skipWhile

Observable 序列的開頭跳過發射滿足條件的元素

image.png ``` 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 序列發射一個元素。

image.png

``` let disposeBag = DisposeBag()

let sourceSequence = PublishSubject() let referenceSequence = PublishSubject()

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