-
Ch4. Filtering OperatorsRay Wenderlich/Combine 2021. 2. 12. 01:44
filter(_:)
let numbers = (1...10).publisher numbers .filter { $0.isMultiple(of: 3) } .sink(receiveValue: { n in print("\(n) is a multiple of 3!") }) .store(in: &subscriptions)
3 is a multiple of 3! 6 is a multiple of 3! 9 is a multiple of 3!
removeDuplicates()
let words = "hey hey there! want to listen to mister mister ?" .components(separatedBy: " ") .publisher words .removeDuplicates() .sink(receiveValue: { print($0) }) .store(in: &subscriptions)
hey there! want to listen to mister ?
compactMap(_:)
let strings = ["a", "1.24", "3", "def", "45", "0.23"].publisher strings .compactMap { Float($0) } .sink(receiveValue: { print($0) }) .store(in: &subscriptions)
1.24 3.0 45.0 0.23
ignoreOutput()
let numbers = (1...10_000).publisher numbers .ignoreOutput() .sink(receiveCompletion: { print("Completed with: \($0)") }, receiveValue: { print($0) }) .store(in: &subscriptions)
Completed with: finished
Finding values
first(where:_)
let numbers = (1...9).publisher numbers .print("numbers") .first(where: { $0 % 2 == 0 }) .sink(receiveCompletion: { print("Completed with: \($0)") }, receiveValue: { print($0) }) .store(in: &subscriptions)
numbers: receive subscription: (1...9) numbers: request unlimited numbers: receive value: (1) numbers: receive value: (2) numbers: receive cancel 2 Completed with: finished
로그에서 볼 수 있듯이 3부터는 이벤트도 전달되지 않았다. 2를 찾고 바로 시퀀스 종료
last(where:_)
let numbers = (1...9).publisher numbers .last(where: { $0 % 2 == 0 }) .sink(receiveCompletion: { print("Completed with: \($0)") }, receiveValue: { print($0) }) .store(in: &subscriptions)
8 Completed with: finished
last(where:)은 first(where:)과 반대로 시퀀스가 종료되어야 찾고자 하는 값을 찾을 수 있다.
Dropping values
RxSwift의 Skip과 동일한 기능
let numbers = (1...10).publisher numbers .dropFirst(8) .sink(receiveValue: { print($0) }) .store(in: &subscriptions)
9 10
drip(while:_)
let numbers = (1...10).publisher numbers .drop(while: { $0 % 5 != 0 }) .sink(receiveValue: { print($0) }) .store(in: &subscriptions)
5 6 7 8 9 10
drop(untilOutputFrom:_)
et isReady = PassthroughSubject<Void, Never>() let taps = PassthroughSubject<Int, Never>() taps .drop(untilOutputFrom: isReady) .sink(receiveValue: { print($0) }) .store(in: &subscriptions) (1...5).forEach { n in taps.send(n) if n == 3 { isReady.send() } }
4 5
Limiting values
let numbers = (1...10).publisher numbers .prefix(2) .sink(receiveCompletion: { print("Completed with: \($0)") }, receiveValue: { print($0) }) .store(in: &subscriptions)
1 2 Completed with: finished
prefix(while:_)
let numbers = (1...10).publisher numbers .prefix(while: { $0 < 3 }) .sink(receiveCompletion: { print("Completed with: \($0)") }, receiveValue: { print($0) }) .store(in: &subscriptions)
1 2 Completed with: finished
prefix(until:_)
let isReady = PassthroughSubject<Void, Never>() let taps = PassthroughSubject<Int, Never>() taps .prefix(untilOutputFrom: isReady) .sink(receiveCompletion: { print("Completed with: \($0)") }, receiveValue: { print($0) }) .store(in: &subscriptions) (1...5).forEach { n in taps.send(n) if n == 2 { isReady.send() } }
1 2 Completed with: finished
'Ray Wenderlich > Combine' 카테고리의 다른 글
Ch5. Combining Operators (0) 2021.02.12 Ch3. Transforming Operators (0) 2021.01.27 Ch2. Publishers & Subscribers (0) 2021.01.27 Ch1. Hello, Combine! (0) 2021.01.27