DataDecoder

open class DataDecoder

Decodes data from a network response.

Subspec: Utility/HTTPClient

Alamofire.request(request.endpoint).responseJSON { (response) in
    completion(self.decoder.decodeData(response.data))
}

The data decoder can be used with network requests that return a Data object. The DataDecoder uses the APIErrorType to identify the network errors. If the object can not be deserialized the decoder will print out which coding key failed the the deserialization. Integration with the killswitch is also available if the api error received matched the unique killswitch or force update identifier.

  • JSON Decoder used to decode the object.

    Declaration

    Swift

    private let decoder: JSONDecoder
  • Optional killswitch provider used to check if the killswitch is active.

    Declaration

    Swift

    private let killSwitchProvider: KillSwitchProvider?
  • Default initializer.

    Declaration

    Swift

    public init(decoder: JSONDecoder = JSONDecoder(), killSwitchProvider: KillSwitchProvider? = nil)

    Parameters

    decoder

    JSON Decoder to decode objects with.

    killSwitchProvider

    Optional KillSwitchProvider.

  • Decodes the data into the desired result.

    Declaration

    Swift

    public func decodeData<T>(_ data: Data?) -> Result<T> where T : Decodable

    Parameters

    data

    Data to deserialize.

    Return Value

    Desired result with the deserialized object.

  • Decodes the error if the decoder was unable to decode the object type requested.

    Declaration

    Swift

    func decodeError<T>(_ data: Data, decodeError: Error) -> Result<T> where T : Decodable

    Parameters

    data

    Data of the request.

    decodeError

    Optional error from the json decoder.

    Return Value

    The failed result of the request.