μ λ€λ¦ ꡬ쑰체 | μ 곡λ ν΄λ‘μ λ₯Ό μ¬μ©νμ¬ μμμ νλ₯΄λ Publisherλ‘λΆν° λͺ¨λ μμλ€μ λ³ννλ Publisher
μ΄λμ
λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisher
transform
: μμμ νλ₯΄λ Publisherλ‘λΆν° μμλ€μ λ³ννκΈ° μν ν΄λ‘μ
λ³ν ν¨μλ₯Ό μ μ©νμ¬ μμλ₯Ό λ³ννκΈ° μν΄ μ¬μ©νλ€.
Swiftμ map
ν¨μμ κ°μ λμμ νλ€.
map
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
// 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
Publishers.Sequence
Publisherμμ 1κ³Ό 2μ κ°μ΄ μ°¨λ‘λλ‘ λμ€κ³ , μ΄ μμμ λνμ¬ 2λ₯Ό κ³±νλ λ³ν ν¨μλ₯Ό μ μ©νμ¬ 2μ 4μ κ°μ λΈ ν μ’
λ£νλ€.
RxSwift
map
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
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
ReactiveSwift
map
μ€νΌλ μ΄ν°λ₯Ό μ¬μ©νμ¬ κ΅¬νν μ μλ€.
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
μ°Έκ³
ReactiveX - Operators - Map