Create a Result from optional value-error pair

Swift

If you use ObjC APIs that have optionals for both value and error in their callback handlers, this extension to map them into a Result type might be useful.

swift

public extension Result { init(value: Success?, error: Failure?) { switch (value, error) { case (let value?, _): self = .success(value) case (_, let error?): self = .failure(error) default: fatalError("Both value and error are nil") // or fallback to a generic error } } }