이니셜라이저는 두 개의 인자를 받는다.
변환 함수를 적용하여 요소를 변환하기 위해 사용한다.
// Publishers.Map Publisher
Publishers.Map(upstream: Publishers.Sequence<[Int], Never>(sequence: [1, 2])) { $0 * 2 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Map Error")
case .finished:
print("Combine Map Finish")
}
}, receiveValue: { number in
print("Combine Map : \(number)")
})
.store(in: &cancellables)
// map Operator
Publishers.Sequence<[Int], Never>(sequence: [1, 2])
.map { $0 * 2 }
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Map Error")
case .finished:
print("Combine Map Finish")
}
}, receiveValue: { number in
print("Combine Map : \(number)")
})
.store(in: &cancellables)
// Combine Map : 2
// Combine Map : 4
// Combine Map Finish
Observable.from([1, 2])
.map { $0 * 2 }
.subscribe(onNext: { number in
print("RxSwift Map : \(number)")
}, onError: { _ in
print("RxSwift Map Error")
}, onCompleted: {
print("RxSwift Map Finish")
})
.disposed(by: disposeBag)
// RxSwift Map : 2
// RxSwift Map : 4
// RxSwift Map Finish
SignalProducer([1, 2])
.map { $0 * 2 }
.start { event in
switch event {
case let .value(number):
print("ReactiveSwift Map : \(number)")
case .failed:
print("ReactiveSwift Map Error")
case .completed:
print("ReactiveSwift Map Finish")
default:
break
}
}
// ReactiveSwift Map : 2
// ReactiveSwift Map : 4
// ReactiveSwift Map Finish