이니셜라이저는 한 개의 인자를 받는다.
// Publishers.Last Publisher
Publishers
.Last(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4, 5]))
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Last Error")
case .finished:
print("Combine Last Finish")
}
}, receiveValue: { value in
print("Combine Last : \(value)")
})
.store(in: &cancellables)
// last Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2, 3, 4, 5])
.last()
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Last Error")
case .finished:
print("Combine Last Finish")
}
}, receiveValue: { value in
print("Combine Last : \(value)")
})
.store(in: &cancellables)
// Combine Last : 5
// Combine Last Finish
상위 Publisher는 1, 2, 3, 4, 5의 값을 차례대로 낸다.
Observable.from([1, 2, 3, 4, 5])
.takeLast(1)
.subscribe(onNext: { value in
print("RxSwift Last : \(value)")
}, onError: { _ in
print("RxSwift Last Error")
}, onCompleted: {
print("RxSwift Last Finish")
})
.disposed(by: disposeBag)
// RxSwift Last : 5
// RxSwift Last Finish
SignalProducer([1, 2, 3])
.last()?.signal
.observe { event in
switch event {
case let .value(value):
print("ReactiveSwift Last : \(value)")
case .failed:
print("ReactiveSwift Last Error")
case .completed:
print("ReactiveSwift Last Finish")
default:
break
}
}
// ReactiveSwift Last : 1
// ReactiveSwift Last Finish