原文:Apple Developer Documentation
文档摘录翻译

Result 定义

1
@frozen enum Result<Success, Failure> where Failure : Error

Result 应用

编写可能失败Failable的异步API

编写可能失败的函数、方法或其他API时,可以在声明中使用throws关键字来标示此API可抛出异常。但是我们不能将throws关键字用于异步返回的API,此时可用Result枚举来保存异步调用的结果信息,并利用关联值来携带有关的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
let queue = DispatchQueue(label: “com.example.queue”)

enum EntropyError: Error {
case entropyDepleted
}

struct AsyncRandomGenerator {
static let entropyLimit = 5
var count = 0

mutating func fetchRemoteRandomNumber(
completion: @escaping (Result<Int, EntropyError>) -> Void
) {
let result: Result<Int, EntropyError>
if count < AsyncRandomGenerator.entropyLimit {
// Produce numbers until reaching the entropy limit.
result = .success(Int.random(in: 1100))
} else {
// Supply a failure reason when the caller hits the limit.
result = .failure(.entropyDepleted)
}

count += 1

// Delay to simulate an asynchronous source of entropy.
queue.asyncAfter(deadline: .now() + 2) {
completion(result)
}
}
}

保存抛出异常函数的结果

有时,我们需要保留函数或其他抛出异常表达式的整个结果。例如,需要序列化结果,或者将其作为值传递给程序的其他部分。在这些情况下,请使用Result

转换抛出异常表达式为Result

使用Result枚举的init(catching:)进行转换:

1
let singleSample = Result { try UnreliableRandomGenerator().random() }

转换Result到抛出异常表达式

1
2
3
4
5
6
7
8
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)")
}