DataResponse - SwiftDocOrg/Alamofire GitHub Wiki

DataResponse

Type used to store all values associated with a serialized response of a DataRequest or UploadRequest.

public struct DataResponse<Success, Failure:​ Error>

Inheritance

CustomDebugStringConvertible, CustomStringConvertible

Initializers

init(request:​response:​data:​metrics:​serializationDuration:​result:​)

Creates a DataResponse instance with the specified parameters derived from the response serialization.

public init(request:​ URLRequest?, response:​ HTTPURLResponse?, data:​ Data?, metrics:​ URLSessionTaskMetrics?, serializationDuration:​ TimeInterval, result:​ Result<Success, Failure>)

Parameters

  • request:​ The URLRequest sent to the server.
  • response:​ The HTTPURLResponse from the server.
  • data:​ The Data returned by the server.
  • metrics:​ The URLSessionTaskMetrics of the DataRequest or UploadRequest.
  • serializationDuration:​ The duration taken by serialization.
  • result:​ The Result of response serialization.

Properties

request

The URL request sent to the server.

let request:​ URLRequest?

response

The server's response to the URL request.

let response:​ HTTPURLResponse?

data

The data returned by the server.

let data:​ Data?

metrics

The final metrics of the response.

let metrics:​ URLSessionTaskMetrics?

serializationDuration

The time taken to serialize the response.

let serializationDuration:​ TimeInterval

result

The result of response serialization.

let result:​ Result<Success, Failure>

value

Returns the associated value of the result if it is a success, nil otherwise.

var value:​ Success?

error

Returns the associated error value if the result if it is a failure, nil otherwise.

var error:​ Failure?

description

The textual representation used when written to an output stream, which includes whether the result was a success or failure.

var description:​ String

debugDescription

The debug textual representation used when written to an output stream, which includes the URL request, the URL response, the server data, the duration of the network and serialization actions, and the response serialization result.

var debugDescription:​ String

Methods

map(_:​)

Evaluates the specified closure when the result of this DataResponse is a success, passing the unwrapped result value as a parameter.

public func map<NewSuccess>(_ transform:​ (Success) -> NewSuccess) -> DataResponse<NewSuccess, Failure>

Use the map method with a closure that does not throw. For example:​

let possibleData:​ DataResponse<Data> = ...
let possibleInt = possibleData.map { $0.count }

Parameters

  • transform:​ A closure that takes the success value of the instance's result.

Returns

A DataResponse whose result wraps the value returned by the given closure. If this instance's result is a failure, returns a response wrapping the same failure.

tryMap(_:​)

Evaluates the given closure when the result of this DataResponse is a success, passing the unwrapped result value as a parameter.

public func tryMap<NewSuccess>(_ transform:​ (Success) throws -> NewSuccess) -> DataResponse<NewSuccess, Error>

Use the tryMap method with a closure that may throw an error. For example:​

let possibleData:​ DataResponse<Data> = ...
let possibleObject = possibleData.tryMap {
    try JSONSerialization.jsonObject(with:​ $0)
}

Parameters

  • transform:​ A closure that takes the success value of the instance's result.

Returns

A success or failure DataResponse depending on the result of the given closure. If this instance's result is a failure, returns the same failure.

mapError(_:​)

Evaluates the specified closure when the DataResponse is a failure, passing the unwrapped error as a parameter.

public func mapError<NewFailure:​ Error>(_ transform:​ (Failure) -> NewFailure) -> DataResponse<Success, NewFailure>

Use the mapError function with a closure that does not throw. For example:​

let possibleData:​ DataResponse<Data> = ...
let withMyError = possibleData.mapError { MyError.error($0) }

Parameters

  • transform:​ A closure that takes the error of the instance.

Returns

A DataResponse instance containing the result of the transform.

tryMapError(_:​)

Evaluates the specified closure when the DataResponse is a failure, passing the unwrapped error as a parameter.

public func tryMapError<NewFailure:​ Error>(_ transform:​ (Failure) throws -> NewFailure) -> DataResponse<Success, Error>

Use the tryMapError function with a closure that may throw an error. For example:​

let possibleData:​ DataResponse<Data> = ...
let possibleObject = possibleData.tryMapError {
    try someFailableFunction(taking:​ $0)
}

Parameters

  • transform:​ A throwing closure that takes the error of the instance.

Returns

A DataResponse instance containing the result of the transform.

⚠️ **GitHub.com Fallback** ⚠️