Result
public enum Result<Value>
extension Result: CustomStringConvertible
extension Result: CustomDebugStringConvertible
                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.
- 
                  
                  
Declaration
Swift
case success(Value) - 
                  
                  
Declaration
Swift
case failure(Error) - 
                  
                  
Returns
trueif the result is a success,falseotherwise.Declaration
Swift
public var isSuccess: Bool { get } - 
                  
                  
Returns
trueif the result is a failure,falseotherwise.Declaration
Swift
public var isFailure: Bool { get } - 
                  
                  
Returns the associated value if the result is a success,
nilotherwise.Declaration
Swift
public var value: Value? { get } - 
                  
                  
Returns the associated error value if the result is a failure,
nilotherwise.Declaration
Swift
public var error: Error? { get } 
- 
                  
                  
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 } 
- 
                  
                  
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 } 
- 
                  
                  
Creates a
Resultinstance 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
valueThe closure to execute and create the result for.
 - 
                  
                  
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 errorDeclaration
Swift
public func unwrap() throws -> Value - 
                  
                  
Evaluates the specified closure when the
Resultis a success, passing the unwrapped value as a parameter.Use the
mapmethod 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 errorDeclaration
Swift
public func map<T>(_ transform: (Value) -> T) -> Result<T>Parameters
transformA closure that takes the success value of the
Resultinstance.Return Value
A
Resultcontaining the result of the given closure. If this instance is a failure, returns the same failure. - 
                  
                  
Evaluates the specified closure when the
Resultis a success, passing the unwrapped value as a parameter.Use the
flatMapmethod 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
transformA closure that takes the success value of the instance.
Return Value
A
Resultcontaining the result of the given closure. If this instance is a failure, returns the same failure. - 
                  
                  
Evaluates the specified closure when the
Resultis a failure, passing the unwrapped error as a parameter.Use the
mapErrorfunction 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 : ErrorParameters
transformA closure that takes the error of the instance.
Return Value
A
Resultinstance containing the result of the transform. If this instance is a success, returns the same instance. - 
                  
                  
Evaluates the specified closure when the
Resultis a failure, passing the unwrapped error as a parameter.Use the
flatMapErrorfunction 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 : ErrorParameters
transformA throwing closure that takes the error of the instance.
Return Value
A
Resultinstance containing the result of the transform. If this instance is a success, returns the same instance. - 
                  
                  
Evaluates the specified closure when the
Resultis a success, passing the unwrapped value as a parameter.Use the
withValuefunction to evaluate the passed closure without modifying theResultinstance.Declaration
Swift
@discardableResult public func withValue(_ closure: (Value) throws -> Void) rethrows -> ResultParameters
closureA closure that takes the success value of this instance.
Return Value
This
Resultinstance, unmodified. - 
                  
                  
Evaluates the specified closure when the
Resultis a failure, passing the unwrapped error as a parameter.Use the
withErrorfunction to evaluate the passed closure without modifying theResultinstance.Declaration
Swift
@discardableResult public func withError(_ closure: (Error) throws -> Void) rethrows -> ResultParameters
closureA closure that takes the success value of this instance.
Return Value
This
Resultinstance, unmodified. - 
                  
                  
Evaluates the specified closure when the
Resultis a success.Use the
ifSuccessfunction to evaluate the passed closure without modifying theResultinstance.Declaration
Swift
@discardableResult public func ifSuccess(_ closure: () throws -> Void) rethrows -> ResultParameters
closureA
Voidclosure.Return Value
This
Resultinstance, unmodified. - 
                  
                  
Evaluates the specified closure when the
Resultis a failure.Use the
ifFailurefunction to evaluate the passed closure without modifying theResultinstance.Declaration
Swift
@discardableResult public func ifFailure(_ closure: () throws -> Void) rethrows -> ResultParameters
closureA
Voidclosure.Return Value
This
Resultinstance, unmodified. 
      Result Enumeration Reference