μ λ€λ¦ ꡬ쑰체 | μ 곡λ ν΄λ‘μ μ μΌμΉνλ λͺ¨λ μμλ₯Ό λ€μ λ°ννλ Publisher
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher.
isIncluded
: μ΄λ€ μμλ₯Ό λ€μ λ°νν μ§λ₯Ό λνλ΄λ ν΄λ‘μ . ν΄λ‘μ μ μ€ν κ²°κ³Όλ Bool νμ
μ΄λ©°, trueμ΄λ©΄ ν΄λΉ μμλ₯Ό λ€μ λ°ννλ€.
쑰건μ λ§λ μμλ§ λ¨κΈ°κ³ μΆμ λ μ¬μ©ν μ μλ€.
Swiftμ filter
ν¨μμ κ°μ λμμ νλ€.
filter
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// Publishers.Filter Publisher
Publishers.Filter(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4])) { $0.isMultiple(of: 2) }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Filter Error")
case .finished:
print("Combine Filter Finish")
}
}, receiveValue: { value in
print("Combine Filter : \(value)")
})
.store(in: &cancellables)
// filter Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4])
.filter { $0.isMultiple(of: 2) }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Filter Error")
case .finished:
print("Combine Filter Finish")
}
}, receiveValue: { value in
print("Combine Filter : \(value)")
})
.store(in: &cancellables)
// Combine Filter : 2
// Combine Filter : 4
// Combine Filter Finish
μνμ€μμ 2μ λ°°μμΈ κ²λ§ λ€μ λ°ννκΈ° μν΄ isMultiple(of: 2)
λ₯Ό μ¬μ©νμλ€.
κ²°κ³Όμ μΌλ‘ 1, 2, 3, 4μ κ°μμ 2, 4μ κ°λ§ λ€μ λ°νλλ€.
RxSwift
filter
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.from([1, 2, 3, 4])
.filter { $0.isMultiple(of: 2) }
.subscribe(onNext: { value in
print("RxSwift Filter : \(value)")
}, onError: { _ in
print("RxSwift Filter Error")
}, onCompleted: {
print("RxSwift Filter Finish")
})
.disposed(by: disposeBag)
// RxSwift Filter : 2
// RxSwift Filter : 4
// RxSwift Filter Finish
ReactiveSwift
filter
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
SignalProducer([1, 2, 3, 4])
.filter { $0.isMultiple(of: 2) }
.start { event in
switch event {
case let .value(value):
print("ReactiveSwift Filter : \(value)")
case .failed:
print("ReactiveSwift Filter")
case .completed:
print("ReactiveSwift Filter")
default:
break
}
}
// ReactiveSwift Filter : 2
// ReactiveSwift Filter : 4
// ReactiveSwift Filter Finish
μ°Έκ³
ReactiveX - Operators - Filter