이니셜라이저는 두 개의 인자를 받는다.
// 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의 값을 차례대로 낸다.
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
해당 동작을 구현하기 위한 오퍼레이터를 제공하지 않는다.