Result

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

    Declaration

    Swift

    var success: Success? { get }
  • Returns the associated error value if the result is a failure, nil otherwise.

    Declaration

    Swift

    var failure: Failure? { get }
  • Initializes a Result from value or error. Returns .failure if the error is non-nil, .success otherwise.

    Declaration

    Swift

    init(value: Success, error: Failure?)

    Parameters

    value

    A value.

    error

    An Error.

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

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

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

    Declaration

    Swift

    func tryMap<NewSuccess>(_ transform: (Success) throws -> NewSuccess) -> Result<NewSuccess, Error>

    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.

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

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

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

    Declaration

    Swift

    func tryMapError<NewFailure>(_ transform: (Failure) throws -> NewFailure) -> Result<Success, Error> where NewFailure : 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 success.