RxSwift文档翻译5 -- Filtering and Conditional Operators

前言

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

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

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 88,924评论 13 123
  • 1.RxSwift初识 RxSwift是 ReactiveX 的swift版本,是一种函数式响应编程的框架。RxS...
    MrMessy阅读 2,248评论 0 9
  • 原创 2017-06-13 RxSwift 这个框架RP框架相信你应该不陌生了,在Objective-C中我们使用...
    一根聪阅读 2,733评论 4 8
  • 时间:2017.1.23上午10:00 地点:魁玉路开心小镇 绘本来源:第二书房“百城千群万里书香”漂流书包 带领...
    舒颐阅读 123评论 2 2
  • 编译器指令用@开始 @"chars" 实现常量字符串对象@class c1,c2,... 将 c1,c2..声明为...
    OneKeyRestore阅读 16评论 0 0