Skipping operators
ignoreElements
next不通过,.completed or .error通过
ignore .next event elements. It will, however, allow through stop events, i.e.,
.completed or .error events.
example(of: "ignoreElements") {
// 1
let strikes = PublishSubject<String>()
let disposeBag = DisposeBag()
// 2
strikes
.ignoreElements()
.subscribe { _ in
print("You're out!")
}
.addDisposableTo(disposeBag)
strikes.onNext("X")
strikes.onNext("X")
strikes.onNext("X")
strikes.onCompleted()
}
--- Example of: ignoreElements ---
You're out!
elementAt
只处理在指定位置的事件
example(of: "elementAt") {
// 1
let strikes = PublishSubject<String>()
let disposeBag = DisposeBag()
// 2
strikes
.elementAt(2)
.subscribe(onNext: { _ in
print("You're out!")
})
.addDisposableTo(disposeBag)
strikes.onNext("X")
strikes.onNext("X")
strikes.onNext("X")
}
--- Example of: elementAt ---
You're out!
filter
过滤掉某些不符合要求的事件
example(of: "filter") {
let disposeBag = DisposeBag()
// 1
Observable.of(1, 2, 3, 4, 5, 6)
// 2
.filter { integer in
integer % 2 == 0
}
// 3
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: filter ---
2
4
6
Skipping operators
skip
取消前几个事件
example(of: "skip") {
let disposeBag = DisposeBag()
// 1
Observable.of("A", "B", "C", "D", "E", "F")
// 2
.skip(3)
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: skip ---
D
E
F
skipWhile
从开始,满足条件的事件消息都取消,直到不满足时之后的都通过
example(of: "skipWhile") {
let disposeBag = DisposeBag()
// 1
Observable.of(2, 2, 3, 4, 4)
// 2
.skipWhile { integer in
integer % 2 == 0
}
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: skipWhile ---
3
4
4
skipUntil
直到某个sequence发出了事件消息,才开始接收当前sequence发出的事件消息
example(of: "skipUntil") {
let disposeBag = DisposeBag()
// 1
let subject = PublishSubject<String>()
let trigger = PublishSubject<String>()
// 2
subject
.skipUntil(trigger)
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
subject.onNext("A")
subject.onNext("B")
trigger.onNext("X")
subject.onNext("C")
}
--- Example of: skipUntil ---
C
Taking operators
take
只处理前几个事件信号
example(of: "take") {
let disposeBag = DisposeBag()
// 1
Observable.of(1, 2, 3, 4, 5, 6)
// 2
.take(3)
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: take ---
1
2
3
takeWhileWithIndex
只要不满足条件就停止
example(of: "takeWhileWithIndex") {
let disposeBag = DisposeBag()
// 1
Observable.of(2, 2, 4, 4, 6, 6)
// 2
.takeWhileWithIndex { integer, index in
// 3
integer % 2 == 0 && index < 3
}
// 4
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: takeWhileWithIndex ---
2
2
4
takeUntil
接收事件消息,直到另一个sequence发出事件消息的时候就终止
example(of: "takeUntil") {
let disposeBag = DisposeBag()
// 1
let subject = PublishSubject<String>()
let trigger = PublishSubject<String>()
// 2
subject
.takeUntil(trigger)
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
// 3
subject.onNext("1")
subject.onNext("2")
trigger.onNext("X")
subject.onNext("3")
}
--- Example of: takeUntil ---
1
2
Distinct operators
distinctUntilChanged
当下一个事件与前一个事件是不同的事件才进行处理操作
example(of: "distinctUntilChanged") {
let disposeBag = DisposeBag()
// 1
Observable.of("A", "A", "B", "B", "A")
// 2
.distinctUntilChanged()
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: distinctUntilChanged ---
A
B
A
example(of: "distinctUntilChanged(_:)") {
let disposeBag = DisposeBag()
// 1
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
// 2
Observable<NSNumber>.of(10, 110, 20, 200, 210, 310)
// 3
.distinctUntilChanged { a, b in
// 4
guard let aWords = formatter.string(from: a)?.components(separatedBy: " "),
let bWords = formatter.string(from: b)?.components(separatedBy: " ")
else {
return false
}
var containsMatch = false
// 5
for aWord in aWords {
for bWord in bWords {
if aWord == bWord {
containsMatch = true
break
}
}
}
return containsMatch
}
// 4
.subscribe(onNext: {
print($0)
})
.addDisposableTo(disposeBag)
}
--- Example of: distinctUntilChanged(_:) ---
10
20
200
网友评论