杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
//  Decode.swift
//  RxSwift
//
//  Created by Shai Mishali on 24/07/2020.
//  Copyright © 2020 Krunoslav Zaher. All rights reserved.
//
 
import Foundation
 
public extension ObservableType where Element == Data {
  /// Attempt to decode the emitted `Data` using a provided decoder.
  ///
  /// - parameter type: A `Decodable`-conforming type to attempt to decode to
  /// - parameter decoder: A capable decoder, e.g. `JSONDecoder` or `PropertyListDecoder`
  ///
  /// - note: If using a custom decoder, it must conform to the `DataDecoder` protocol.
  ///
  /// - returns: An `Observable` of the decoded type
  func decode<Item: Decodable,
              Decoder: DataDecoder>(type: Item.Type,
                                    decoder: Decoder) -> Observable<Item> {
    map { try decoder.decode(type, from: $0) }
  }
}
 
/// Represents an entity capable of decoding raw `Data`
/// into a concrete `Decodable` type
public protocol DataDecoder {
  func decode<Item: Decodable>(_ type: Item.Type, from data: Data) throws -> Item
}
 
extension JSONDecoder: DataDecoder {}
extension PropertyListDecoder: DataDecoder {}