PeakResult 2.2.1

PeakResult 2.2.1

Sam Oakley 维护。



  • Sam Oakley

Peak Result

PeakResult 是一个 Swift 微框架,提供简单的 Result 类型。它是 Peak 框架 的一部分。

Result 类型非常简单:它表示成功,包含表示成功结果的关联值;或者失败,包含关联的错误。这对于表示可能会失败的操作非常完美。

示例

// Both are Result<String> even though one has a string and the other contains an error
let niceResult = Result { return "Hello!" }
let throwingResult = Result { throw TestError.justATest }

使用 switch-case 解包

func throwingMethod() -> String throws { ... }
let result = Result { throwingMethod() }

switch result {
case .success(let value):
    print(value) // value is String
case .failure(let error):
    print(error.localizedDescription) // handle any error
}

使用 do-try 解包

func throwingMethod() -> String throws { ... }
let result = Result { throwingMethod() }

do {
    let value = try result.resolve() // value is String
} catch {
    print(error.localizedDescription) // handle any error
}

请看包含的测试以获取更多示例。

入门

安装

  • 使用 Cocoapods,将 pod 'PeakResult' 添加到您的 Podfile。
  • 在需要的位置导入 import PeakResult

贡献

请阅读 CONTRIBUTING.md 以获取关于我们行为准则的详细信息,以及向我们提交拉取请求的流程。

版本控制

我们使用 SemVer 进行版本控制。

许可证

本项目采用 MIT 许可证,详情请参阅 LICENSE.md 文件。

鸣谢

Peak 框架

Peak 框架是由 3Squared 团队创建的一系列开源微框架的集合,以 Peak District 命名。它包括以下内容:

名称 描述
PeakCoreData Core Data 提供增强和便利性。
PeakOperation Operation 提供增强和便利性,利用 Result 类型。
PeakNetwork 一个基于 Session 的网络框架,使用 PeakOperation,并利用 Codable 的能力。