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?