Publishers.Encode

μ œλ„€λ¦­ ꡬ쑰체

μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” 두 개의 인자λ₯Ό λ°›λŠ”λ‹€.

  • upstream: μƒμœ„μ— 흐λ₯΄λŠ” Publisher

  • encoder : μ‚¬μš©ν•  인코더

encoderλŠ” Combine의 TopLevelEncoder ν”„λ‘œν† μ½œμ„ 채택해야 ν•˜λŠ”λ°, ν˜„μž¬ JSONEncoder와 PropertyListEncoderκ°€ 이 ν”„λ‘œν† μ½œμ„ μ±„νƒν•˜κ³  μžˆλ‹€.

μƒμœ„ 슀트림의 μš”μ†Œλ₯Ό μΈμ½”λ”©ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•  수 μžˆλ‹€.

encode μ˜€νΌλ ˆμ΄ν„°μ™€ 관련이 μžˆλ‹€.

struct SimpleJSON: Encodable {
  let key: String
}
let simpleJSON = SimpleJSON(key: "value")

// Publishers.Encode Publisher
Publishers.Encode(upstream: Just(simpleJSON), encoder: JSONEncoder())
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Encode Error")
    case .finished:
      print("Combine Encode Finish")
    }
  }, receiveValue: { value in
    print("Combine Encode : \(value)")
  })
  .store(in: &cancellables)

// encode Operator
Just(simpleJSON)
  .encode(encoder: JSONEncoder())
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Encode Error")
    case .finished:
      print("Combine Encode Finish")
    }
  }, receiveValue: { value in
    print("Combine Encode : \(value)")
  })
  .store(in: &cancellables)

// Combine Encode : 15 bytes
// Combine Encode Finish

μƒμœ„ PublisherλŠ” Encodable ν”„λ‘œν† μ½œμ„ λ”°λ₯΄λŠ” ꡬ쑰체의 μΈμŠ€ν„΄μŠ€λ₯Ό λ°œν–‰ν•˜λ©°, JSONEncoderλ₯Ό μ‚¬μš©ν•˜μ—¬ μΈμ½”λ”©ν•œλ‹€.

RxSwift

ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

ReactiveSwift

ν•΄λ‹Ή κΈ°λŠ₯을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•œ μ˜€νΌλ ˆμ΄ν„°λ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠλŠ”λ‹€.

Last updated