DataResponse - SwiftDocOrg/Alamofire GitHub Wiki
Type used to store all values associated with a serialized response of a DataRequest or UploadRequest.
public struct DataResponse<Success, Failure:β Error>CustomDebugStringConvertible, CustomStringConvertible
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>)- request:β The
URLRequestsent to the server. - response:β The
HTTPURLResponsefrom the server. - data:β The
Datareturned by the server. - metrics:β The
URLSessionTaskMetricsof theDataRequestorUploadRequest. - serializationDuration:β The duration taken by serialization.
- result:β The
Resultof response serialization.
The URL request sent to the server.
let request:β URLRequest?The server's response to the URL request.
let response:β HTTPURLResponse?The data returned by the server.
let data:β Data?The final metrics of the response.
let metrics:β URLSessionTaskMetrics?The time taken to serialize the response.
let serializationDuration:β TimeIntervalThe result of response serialization.
let result:β Result<Success, Failure>Returns the associated value of the result if it is a success, nil otherwise.
var value:β Success?Returns the associated error value if the result if it is a failure, nil otherwise.
var error:β Failure?The textual representation used when written to an output stream, which includes whether the result was a success or failure.
var description:β StringThe 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:β StringEvaluates 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 }
- transform:β A closure that takes the success value of the instance's result.
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.
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)
}
- transform:β A closure that takes the success value of the instance's result.
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.
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) }
- transform:β A closure that takes the error of the instance.
A DataResponse instance containing the result of the transform.
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)
}
- transform:β A throwing closure that takes the error of the instance.
A DataResponse instance containing the result of the transform.