μ λ€λ¦ ꡬ쑰체 | λ°νλ μμμ μνμ€ λ΄μμ λ²μλ‘ μ§μ λ μμλ€μ λ°ννλ Publisher
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
range
: λ°νν μμκ° μν λ²μ
output
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// 1 : Publishers.Output Publisher
Publishers
.Output(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3]), range: 0 ..< 2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 1
// Combine Output : 2
// Combine Output Finish
// 2 : prefix Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.prefix(2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 1
// Combine Output : 2
// Combine Output Finish
// 3 : output Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.output(at: 1)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 2
// Combine Output Finish
// 4 : output Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3])
.output(in: 0 ..< 2)
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Output Error")
case .finished:
print("Combine Output Finish")
}
}, receiveValue: { value in
print("Combine Output : \(value)")
})
.store(in: &cancellables)
// Combine Output : 1
// Combine Output : 2
// Combine Output Finish
κ° μ½λμ μμ Publisherλ 1, 2, 3μ κ°μ μ°¨λ‘λλ‘ λΈλ€.
1μ μ½λλ 0 ..< 2
μ λ²μμ μν΄ ν΄λΉ λ²μμ μλ 1, 2μ κ°μ λ΄κ³ μ’
λ£νλ€.
2μ μ½λλ prefix(2)
μ μν΄ μ¬μ©νμ¬ μμμλΆν° λ κ°μ μμλ₯Ό λ°ννλ€. λ°λΌμ 1, 2μ κ°μ λ΄κ³ μ’
λ£νλ€.
3μ μ½λλ output(at: 1)
μ μν΄ μ²« λ²μ§Έ μΈλ±μ€μ μλ 2μ κ°μ λ΄κ³ μ’
λ£νλ€.
4μ μ½λλ output(in: 0 ..< 2)
μ μν΄ λ²μμ μλ 1, 2μ κ°μ λ΄κ³ μ’
λ£νλ€.
RxSwift
elementAt
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
Observable.from([1, 2, 3])
.elementAt(1)
.subscribe(onNext: { value in
print("RxSwift Output : \(value)")
}, onError: { _ in
print("RxSwift Output Error")
}, onCompleted: {
print("RxSwift Output Finish")
})
.disposed(by: disposeBag)
// RxSwift Output : 2
// RxSwift Output Finish
ReactiveSwift
ν΄λΉ λμμ ꡬννκΈ° μν μ€νΌλ μ΄ν°λ₯Ό μ 곡νμ§ μλλ€.
μ°Έκ³
ReactiveX - Operators - ElementAt