Generic Instance Method

map(_:)

Returns a new result, mapping any success value using the given transformation.

Declaration

func map<NewSuccess>(_ transform: (Success) -> NewSuccess) -> Result<NewSuccess, Failure>

Parameters

transform

A closure that takes the success value of this instance.

Return Value

A Result instance with the result of evaluating transform as the new success value if this instance represents a success.

Discussion

Use this method when you need to transform the value of a Result instance when it represents a success. The following example transforms the integer success value of a result into a string:

func getNextInteger() -> Result<Int, Error> { /* ... */ }

let integerResult = getNextInteger()
// integerResult == .success(5)
let stringResult = integerResult.map({ String($0) })
// stringResult == .success("5")

See Also

Transforming a Result

func mapError<NewFailure>((Failure) -> NewFailure) -> Result<Success, NewFailure>

Returns a new result, mapping any failure value using the given transformation.

func flatMap<NewSuccess>((Success) -> Result<NewSuccess, Failure>) -> Result<NewSuccess, Failure>

Returns a new result, mapping any success value using the given transformation and unwrapping the produced result.

func flatMapError<NewFailure>((Failure) -> Result<Success, NewFailure>) -> Result<Success, NewFailure>

Returns a new result, mapping any failure value using the given transformation and unwrapping the produced result.