Back to Alamofire

Result

docs/docsets/Alamofire.docset/Contents/Resources/Documents/Enums/Result.html

5.12.08.8 KB
Original Source

Result

public enum Result<Value>

Used to represent whether a request was successful or encountered an error.

  • success: The request and all post processing operations were successful resulting in the serialization of the provided associated value.

  • failure: The request encountered an error resulting in a failure. The associated values are the original data provided by the server as well as the error that caused the failure.

`

                success(_:)
                `

Undocumented

Declaration

Swift

case success(Value)

`

                failure(_:)
                `

Undocumented

Declaration

Swift

case failure(Error)

`

                isSuccess
                `

Returns true if the result is a success, false otherwise.

Declaration

Swift

public var isSuccess: Bool { get }

`

                isFailure
                `

Returns true if the result is a failure, false otherwise.

Declaration

Swift

public var isFailure: Bool { get }

`

                value
                `

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

Declaration

Swift

public var value: Value? { get }

`

                error
                `

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

Declaration

Swift

public var error: Error? { get }

[

CustomStringConvertible

](#/CustomStringConvertible)

`

                description
                `

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

Declaration

Swift

public var description: String { get }

[

CustomDebugStringConvertible

](#/CustomDebugStringConvertible)

`

                debugDescription
                `

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

Declaration

Swift

public var debugDescription: String { get }

[

Functional APIs

](#/Functional%20APIs)

`

                init(value:)
                `

Creates a Result instance from the result of a closure.

A failure result is created when the closure throws, and a success result is created when the closure succeeds without throwing an error.

func someString() throws -> String { ... }

let result = Result(value: {
    return try someString()
})

// The type of result is Result<String>

The trailing closure syntax is also supported:

let result = Result { try someString() }

Declaration

Swift

public init(value: () throws -> Value)

Parameters

| value |

The closure to execute and create the result for.

|

`

                unwrap()
                `

Returns the success value, or throws the failure error.

let possibleString: Result<String> = .success("success")
try print(possibleString.unwrap())
// Prints "success"

let noString: Result<String> = .failure(error)
try print(noString.unwrap())
// Throws error

Declaration

Swift

public func unwrap() throws -> Value

`

                map(_:)
                `

Evaluates the specified closure when the Result is a success, passing the unwrapped value as a parameter.

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

let possibleData: Result<Data> = .success(Data())
let possibleInt = possibleData.map { $0.count }
try print(possibleInt.unwrap())
// Prints "0"

let noData: Result<Data> = .failure(error)
let noInt = noData.map { $0.count }
try print(noInt.unwrap())
// Throws error

Declaration

Swift

public func map<T>(_ transform: (Value) -> T) -> Result<T>

Parameters

| transform |

A closure that takes the success value of the Result instance.

|

Return Value

A Result containing the result of the given closure. If this instance is a failure, returns the same failure.

`

                flatMap(_:)
                `

Evaluates the specified closure when the Result is a success, passing the unwrapped value as a parameter.

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

let possibleData: Result<Data> = .success(Data(...))
let possibleObject = possibleData.flatMap {
    try JSONSerialization.jsonObject(with: $0)
}

Declaration

Swift

public func flatMap<T>(_ transform: (Value) throws -> T) -> Result<T>

Parameters

| transform |

A closure that takes the success value of the instance.

|

Return Value

A Result containing the result of the given closure. If this instance is a failure, returns the same failure.

`

                mapError(_:)
                `

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

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

let possibleData: Result<Data> = .failure(someError)
let withMyError: Result<Data> = possibleData.mapError { MyError.error($0) }

Declaration

Swift

public func mapError<T>(_ transform: (Error) -> T) -> Result where T : Error

Parameters

| transform |

A closure that takes the error of the instance.

|

Return Value

A Result instance containing the result of the transform. If this instance is a success, returns the same instance.

`

                flatMapError(_:)
                `

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

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

let possibleData: Result<Data> = .success(Data(...))
let possibleObject = possibleData.flatMapError {
    try someFailableFunction(taking: $0)
}

Declaration

Swift

public func flatMapError<T>(_ transform: (Error) throws -> T) -> Result where T : Error

Parameters

| transform |

A throwing closure that takes the error of the instance.

|

Return Value

A Result instance containing the result of the transform. If this instance is a success, returns the same instance.

`

                withValue(_:)
                `

Evaluates the specified closure when the Result is a success, passing the unwrapped value as a parameter.

Use the withValue function to evaluate the passed closure without modifying the Result instance.

Declaration

Swift

@discardableResult
public func withValue(_ closure: (Value) throws -> Void) rethrows -> Result

Parameters

| closure |

A closure that takes the success value of this instance.

|

Return Value

This Result instance, unmodified.

`

                withError(_:)
                `

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

Use the withError function to evaluate the passed closure without modifying the Result instance.

Declaration

Swift

@discardableResult
public func withError(_ closure: (Error) throws -> Void) rethrows -> Result

Parameters

| closure |

A closure that takes the success value of this instance.

|

Return Value

This Result instance, unmodified.

`

                ifSuccess(_:)
                `

Evaluates the specified closure when the Result is a success.

Use the ifSuccess function to evaluate the passed closure without modifying the Result instance.

Declaration

Swift

@discardableResult
public func ifSuccess(_ closure: () throws -> Void) rethrows -> Result

Parameters

| closure |

A Void closure.

|

Return Value

This Result instance, unmodified.

`

                ifFailure(_:)
                `

Evaluates the specified closure when the Result is a failure.

Use the ifFailure function to evaluate the passed closure without modifying the Result instance.

Declaration

Swift

@discardableResult
public func ifFailure(_ closure: () throws -> Void) rethrows -> Result

Parameters

| closure |

A Void closure.

|

Return Value

This Result instance, unmodified.