Returns a new result, mapping any success value using the given transformation.
SDK
- Xcode 10.2+
Framework
- Swift Standard Library
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")