注意:使用本例中的代码首先应该导入头文件,代码如下:
import RxSwift
Filtering and Conditional Operators
有选择地从源Observable
序列发散元素的操作。
filter
发散一个源Observable
序列中满足指定条件的那些元素。�了解更多
example("filter") {
let disposeBag = DisposeBag()
Observable.of(
"🐱", "🐰", "🐶",
"🐸", "🐱", "🐰",
"🐹", "🐸", "🐱")
.filter {
$0 == "🐱"
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- filter example ---
🐱
🐱
🐱
distinctUntilChanged
压缩一个源Observable
序列中发散的连续重复的元素。�了解更多
example("distinctUntilChanged") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐷", "🐱", "🐱", "🐱", "🐵", "🐱")
.distinctUntilChanged()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- distinctUntilChanged example ---
🐱
🐷
🐱
🐵
🐱
elementAt
发散一个源Observable
序列中指定索引的元素。�了解更多
example("elementAt") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.elementAt(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- elementAt example ---
🐸
single
发出一个源Observable
序列中的第一个元素(或者满足条件的第一个元素)。如果这个源Observable
序列不能发出一个元素或者根据条件查找到两个以上的元素将会�抛出一个错误。
example("single") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.single()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- single example ---
🐱
example("single with conditions") {
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)
}
Debug Area 输出:
--- single with conditions example ---
next(🐸)
completed
next(🐰)
error(Sequence contains more than one element.)
error(Sequence doesn't contain any elements.)
take
从一个源Observable
序列的开始元素�发散指定数量的元素。�了解更多
example("take") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.take(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- take example ---
🐱
🐰
🐶
takeLast
从一个源Observable
序列的结尾元素�发散指定数量的元素。�了解更多
example("takeLast") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.takeLast(3)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- takeLast example ---
🐸
🐷
🐵
takeWhile
发出一个源Observable
序列中�的只要指定条件的值为true
的元素。�了解更多
example("takeWhile") {
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.takeWhile { $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- takeWhile example ---
1
2
3
takeUntil
从一个源Observable
序列中�发散元素直到一个参照Observable
序列发出一个元素。�了解更多
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("🐵")
}
Debug Area 输出:
--- takeUntil example ---
next(🐱)
next(🐰)
next(🐶)
completed
skip
从一个源Observable
序列的开始发散元素,并且跳过指定数量的元素。了解更多
example("skipWhile") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.skip(2)
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- skip example ---
🐶
🐸
🐷
🐵
skipWhile
从一个源Observable
序列的开始发散元素,并且跳过满足条件的元素。了解更多
example("skipWhile") {
let disposeBag = DisposeBag()
Observable.of(1, 2, 3, 4, 5, 6)
.skipWhile { $0 < 4 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- skipWhile example ---
4
5
6
skipWhileWithIndex
从一个源Observable
序列的开始发散元素,并且跳过满足条件的索引的元素。这个闭包查询了每个元素。
example("skipWhileWith") {
let disposeBag = DisposeBag()
Observable.of("🐱", "🐰", "🐶", "🐸", "🐷", "🐵")
.skipWhileWithIndex { element, index in
index < 3
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- skipWhileWithIndex example ---
🐸
🐷
🐵
skipUntil
跳过一个源Observable
序列发送的元素,直到一个参照Observable
序列发出一个元素。了解更多
example("skipUntil") {
let disposeBag = DisposeBag()
let sourceSequence = PublishSubject<String>()
let referenceSequence = PublishSubject<String>()
sourceSequence
.skipUntil(referenceSequence)
.subscribe { print($0) }
.disposed(by: disposeBag)
sourceSequence.onNext("🐱")
sourceSequence.onNext("🐰")
sourceSequence.onNext("🐶")
referenceSequence.onNext("🔴")
sourceSequence.onNext("🐸")
sourceSequence.onNext("🐷")
sourceSequence.onNext("🐵")
}
Debug Area 输出:
--- skipUntil example ---
🐸
🐷
🐵
下一篇: 八、��数学和�集合操作-Mathematical and Aggregate Operators(Rx.playground翻译)�