RXswift

RxSwift文档翻译5 -- Filtering and Co

2017-06-01  本文已影响53人  小白猿

前言

将观察序列的元素值就行筛选的操作

Filtering and Conditional Operators

example("filter") {
    let disposeBag = DisposeBag()
    
    Observable.of(
        "🐱", "🐰", "🐶",
        "🐸", "🐱", "🐰",
        "🐹", "🐸", "🐱")
        .filter {
            $0 == "🐱"
        }
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

// 控制台打印的结果:
--- filter example ---
🐱
🐱
🐱
example("distinctUntilChanged") {
    let disposeBag = DisposeBag()
    
    Observable.of("🐱", "🐷", "🐱", "🐱", "🐱", "🐵", "🐱")
        .distinctUntilChanged()
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

// 控制台打印的结果:
--- distinctUntilChanged example ---
🐱
🐷
🐱
🐵
🐱
example("elementAt") {
    let disposeBag = DisposeBag()
    
    Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
        .elementAt(3)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

// 控制台打印的结果:
--- elementAt example ---
🐸
/*********single 不带条件***********/
example("single") {
            let disposeBag = DisposeBag()
            
            Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                .single()
                .subscribe(onNext: { print($0) })
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- single example ---
🐱

/*********single 带条件***********/
 example("single with conditions") {
            let disposeBag = DisposeBag()
            
            Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                .single { $0 == "🐸" }
                .subscribe { print($0) }
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- single with conditions example ---
next(🐸)
completed

/*********single 带条件,筛选值不唯一***********/
example("single with conditions") {
            let disposeBag = DisposeBag()
            
            Observable.of("🐱", "🐰", "🐶", "🐱", "🐰", "🐶")
                .single { $0 == "🐰" }
                .subscribe { print($0) }
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- single with conditions example ---
next(🐰)
error(Sequence contains more than one element.)

/*********single 带条件,筛选值没有的情况 *****/
example("single with conditions") {
            let disposeBag = DisposeBag()
            
            Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                .single { $0 == "🔵" }
                .subscribe { print($0) }
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- single with conditions example ---
error(Sequence doesn't contain any elements.)
example("take") {
            let disposeBag = DisposeBag()
            
            Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                .take(3)
                .subscribe(onNext: { print($0) })
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- take example ---
🐱
🐰
🐶
example("takeLast") {
    let disposeBag = DisposeBag()
    
    Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
        .takeLast(3)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

example("takeLast") {
    let disposeBag = DisposeBag()
    
    Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
        .takeLast(3)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}
example("takeWhile") {
    let disposeBag = DisposeBag()
    
    Observable.of(1, 2, 3, 4, 5, 6)
        .takeWhile { $0 < 4 }
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}

// 控制台打印的结果:
--- takeWhile example ---
1
2
3
example("takeUntil") {
    let disposeBag = DisposeBag()
    
    let sourceSequence = PublishSubject<String>()
    let referenceSequence = PublishSubject<String>()
    
    sourceSequence
        .takeUntil(referenceSequence)
        .subscribe { print($0) }
        .disposed(by: disposeBag)
    
    sourceSequence.onNext("🐱")
    sourceSequence.onNext("🐰")
    sourceSequence.onNext("🐶")
    
    referenceSequence.onNext("🔴")
    
    sourceSequence.onNext("🐸")
    sourceSequence.onNext("🐷")
    sourceSequence.onNext("🐵")
}

// 控制台打印的结果:
--- takeUntil example ---
next(🐱)
next(🐰)
next(🐶)
completed
example("skip") {
    let disposeBag = DisposeBag()
    
    Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
        .skip(2)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}
// 控制台打印的结果:
--- skip example ---
🐶
🐸
🐷
🐵
example("skipWhile") {
    let disposeBag = DisposeBag()
    
    Observable.of(1, 2, 3, 4, 5, 6)
        .skipWhile { $0 < 4 }
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
}
// 控制台打印的结果:
--- skipWhile example ---
4
5
6
example("skipWhileWithIndex") {
            let disposeBag = DisposeBag()
            
            Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                .skipWhileWithIndex { element, index in
                    index < 3
                }
                .subscribe(onNext: { print($0) })
                .disposed(by: disposeBag)
        }

// 控制台打印的结果:
--- skipWhileWithIndex example ---
🐸
🐷
🐵
example("skipUntil") {
    let disposeBag = DisposeBag()
    
    let sourceSequence = PublishSubject<String>()
    let referenceSequence = PublishSubject<String>()
    
    sourceSequence
        .skipUntil(referenceSequence)
        .subscribe(onNext: { print($0) })
        .disposed(by: disposeBag)
    
    sourceSequence.onNext("🐱")
    sourceSequence.onNext("🐰")
    sourceSequence.onNext("🐶")
    
    referenceSequence.onNext("🔴")
    
    sourceSequence.onNext("🐸")
    sourceSequence.onNext("🐷")
    sourceSequence.onNext("🐵")
}

// 控制台打印的结果:
--- skipUntil example ---
🐸
🐷
🐵

下一节:RxSwift文档翻译6 -- Mathematical and Aggregate Operators

上一篇下一篇

猜你喜欢

热点阅读