美文网首页RXswift
RxSwift文档翻译5 -- Filtering and Co

RxSwift文档翻译5 -- Filtering and Co

作者: 小白猿 | 来源:发表于2017-06-01 17:23 被阅读53次

    前言

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

    Filtering and Conditional Operators

    • filter
      将符合条件的元素值根据条件进行筛选
    example("filter") {
        let disposeBag = DisposeBag()
        
        Observable.of(
            "🐱", "🐰", "🐶",
            "🐸", "🐱", "🐰",
            "🐹", "🐸", "🐱")
            .filter {
                $0 == "🐱"
            }
            .subscribe(onNext: { print($0) })
            .disposed(by: disposeBag)
    }
    
    // 控制台打印的结果:
    --- filter example ---
    🐱
    🐱
    🐱
    
    • distinctUntilChanged
      去掉序列中重复的事件元素,相邻元素如果相同的话,进保留一个,如果不是相邻的元素不做去重元素
    example("distinctUntilChanged") {
        let disposeBag = DisposeBag()
        
        Observable.of("🐱", "🐷", "🐱", "🐱", "🐱", "🐵", "🐱")
            .distinctUntilChanged()
            .subscribe(onNext: { print($0) })
            .disposed(by: disposeBag)
    }
    
    // 控制台打印的结果:
    --- distinctUntilChanged example ---
    🐱
    🐷
    🐱
    🐵
    🐱
    
    • elementAt
      筛选出序列中指定索引的元素
    example("elementAt") {
        let disposeBag = DisposeBag()
        
        Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
            .elementAt(3)
            .subscribe(onNext: { print($0) })
            .disposed(by: disposeBag)
    }
    
    // 控制台打印的结果:
    --- elementAt example ---
    🐸
    
    • single
      分两种情况筛选,当single后面不带条件的时候,则默认筛选第一个元素抛出;single后面带条件就根据条件进行进行筛选,如果元素不是唯一一个或者没有符合条件的元素将抛出错误
    /*********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.)
    
    • take
      仅从可观察序列的开头发出指定数量的元素
    example("take") {
                let disposeBag = DisposeBag()
                
                Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                    .take(3)
                    .subscribe(onNext: { print($0) })
                    .disposed(by: disposeBag)
            }
    
    // 控制台打印的结果:
    --- take example ---
    🐱
    🐰
    🐶
    
    • takeLast
      仅从可观察序列的结尾倒数发出指定数量的元素
    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)
    }
    
    • takeWhile
      仅从可观察序列的开头筛选出指定条件的元素
    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
    
    • takeUntil
      A序列元素等到B序列元素发出元素以后再发出,A序列的事件发送完以后就发发送Complete,B序列无论是否发出新的事件元素以后A序列都不再发出事件元素
    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
    
    • skip
      和take作用相反,仅从可观察序列的开头禁止发出或者排除指定数量的元素
    example("skip") {
        let disposeBag = DisposeBag()
        
        Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
            .skip(2)
            .subscribe(onNext: { print($0) })
            .disposed(by: disposeBag)
    }
    // 控制台打印的结果:
    --- skip example ---
    🐶
    🐸
    🐷
    🐵
    
    • skipWhile
      和takeWhile相反,仅从可观察序列的开头排除出指定条件的元素
    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
    
    • skipWhileWithIndex
      根据索引的条件排除相应的元素
    example("skipWhileWithIndex") {
                let disposeBag = DisposeBag()
                
                Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
                    .skipWhileWithIndex { element, index in
                        index < 3
                    }
                    .subscribe(onNext: { print($0) })
                    .disposed(by: disposeBag)
            }
    
    // 控制台打印的结果:
    --- skipWhileWithIndex example ---
    🐸
    🐷
    🐵
    
    • skipUntil
      和takeUntil相反,A序列事件元素,排除在B序列发出事件元素之前的时间元素,保留B序列发出元素之后的A的事件元素
    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

    相关文章

      网友评论

        本文标题:RxSwift文档翻译5 -- Filtering and Co

        本文链接:https://www.haomeiwen.com/subject/qdmefxtx.html