Result
public enum Result<Success, Failure>
A value that represents either a success or failure, capturing associated values in both cases.
-
A success, storing a
Value
.Declaration
Swift
case success(Success)
-
A failure, storing an
Error
.Declaration
Swift
case failure(Failure)
-
Evaluates the given transform closure when this
Result
instance is.success
, passing the value as a parameter.Use the
map
method with a closure that returns a non-Result
value.Declaration
Swift
public func map<NewSuccess>( _ transform: (Success) -> NewSuccess ) -> Result<NewSuccess, Failure>
Parameters
transform
A closure that takes the successful value of the instance.
Return Value
A new
Result
instance with the result of the transform, if it was applied. -
Evaluates the given transform closure when this
Result
instance is.failure
, passing the error as a parameter.Use the
mapError
method with a closure that returns a non-Result
value.Declaration
Swift
public func mapError<NewFailure>( _ transform: (Failure) -> NewFailure ) -> Result<Success, NewFailure>
Parameters
transform
A closure that takes the failure value of the instance.
Return Value
A new
Result
instance with the result of the transform, if it was applied. -
Evaluates the given transform closure when this
Result
instance is.success
, passing the value as a parameter and flattening the result.Declaration
Swift
public func flatMap<NewSuccess>( _ transform: (Success) -> Result<NewSuccess, Failure> ) -> Result<NewSuccess, Failure>
Parameters
transform
A closure that takes the successful value of the instance.
Return Value
A new
Result
instance, either from the transform or from the previous error value. -
Evaluates the given transform closure when this
Result
instance is.failure
, passing the error as a parameter and flattening the result.Declaration
Swift
public func flatMapError<NewFailure>( _ transform: (Failure) -> Result<Success, NewFailure> ) -> Result<Success, NewFailure>
Parameters
transform
A closure that takes the error value of the instance.
Return Value
A new
Result
instance, either from the transform or from the previous success value.
-
Returns the success value as a throwing expression.
Use this method to retrieve the value of this result if it represents a success, or to catch the value if it represents a failure.
let integerResult: Result<Int, Error> = .success(5) do { let value = try integerResult.get() print("The value is \(value).") } catch error { print("Error retrieving the value: \(error)") } // Prints "The value is 5."
Throws
The failure value, if the instance represents a failure.Declaration
Swift
public func get() throws -> Success
Return Value
The success value, if the instance represents a success.
-
Unwraps the
Result
into a throwing expression.Throws
The error value, if the instance is a failure.Declaration
Swift
@available(*, deprecated, message: "This method will be removed soon. Use `get(﹚ throws -> Success` instead.") public func unwrapped() throws -> Success
Return Value
The success value, if the instance is a success.
-
Creates a new result by evaluating a throwing closure, capturing the returned value as a success, or any thrown error as a failure.
Declaration
Swift
public init(catching body: () throws -> Success)
Parameters
body
A throwing closure to evaluate.
-
Declaration
Swift
public var debugDescription: String { get }