Publishers.Decode
μ λ€λ¦ ꡬ쑰체
μ΄λμ λΌμ΄μ λ λ κ°μ μΈμλ₯Ό λ°λλ€.
upstream
: μμμ νλ₯΄λ Publisherdecoder
: μ¬μ©ν λμ½λ
decoder
λ Combineμ TopLevelDecoder
νλ‘ν μ½μ μ±νν΄μΌ νλλ°, νμ¬ JSONDecoder
μ PropertyListDecoder
κ° μ΄ νλ‘ν μ½μ μ±ννκ³ μλ€.
μμ μ€νΈλ¦Όμ μμλ₯Ό λμ½λ©νκΈ° μν΄ μ¬μ©ν μ μλ€.
decode
μ€νΌλ μ΄ν°μ κ΄λ ¨μ΄ μλ€.
struct SimpleJSON: Decodable {
let key: String
}
let jsonData = "{ \"key\": \"value\" }".data(using: .utf8)!
// Publishers.Decode Publisher
Publishers.Decode<Just<JSONDecoder.Input>, SimpleJSON, JSONDecoder>(upstream: Just(jsonData), decoder: JSONDecoder())
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Decode Error")
case .finished:
print("Combine Decode Finish")
}
}, receiveValue: { value in
print("Combine Decode : \(value)")
})
.store(in: &cancellables)
// encode Operator
Just(jsonData)
.decode(type: SimpleJSON.self, decoder: JSONDecoder())
.sink(receiveCompletion: { completion in
switch completion {
case .failure:
print("Combine Decode Error")
case .finished:
print("Combine Decode Finish")
}
}, receiveValue: { value in
print("Combine Decode : \(value)")
})
.store(in: &cancellables)
// Combine Decode : SimpleJSON(key: "value")
// Combine Decode Finish
μμ Publisherλ JSON νμμ λ°μ΄ν°λ₯Ό κ°λ Data
λ₯Ό λ°ννλ©°, JSONDecoder
λ₯Ό μ¬μ©νμ¬ λμ½λ©νλ€.
RxSwift
ν΄λΉ κΈ°λ₯μ ꡬννκΈ° μν μ€νΌλ μ΄ν°λ₯Ό μ 곡νμ§ μλλ€.
ReactiveSwift
ν΄λΉ κΈ°λ₯μ ꡬννκΈ° μν μ€νΌλ μ΄ν°λ₯Ό μ 곡νμ§ μλλ€.
Last updated
Was this helpful?