OpRes
OpRes.framework
是一个小型库,用于扩展 Swift 的 Optional
和 Result
类型,使其具有 Rust 语言风格。
安装
Carthage
使用 将此行添加到您的 Cartfile
中。
github "sadaie/OpRes"
CocoaPods
使用 将此行添加到您的 Podfile
中。
pod 'OpRes`
使用方法
请参阅 OpResTests/OpResTests.swift
以获取更多详细信息。
可选
import OpRes
let option: Int? = 10
// The first statement is equivalent to the second one.
let x: Int = option.map { $0 * 10 } ?? 0
let y: Int = option.map(or: 0) { $0 * 10 }
print(x == y) // prints `true`
结果
import OpRes
enum MyError: Error {
case someError
}
let option: Int? = 10
let result: Result<Int, MyError> = option.map { $0 * 10 }.ok(or: .someError)
let x: Int = option.map { $0 * 10 }.expect("should be unwrapped")
let y: Int = result.expect("should be unwrapped")
print(x == y) // prints `true`
许可证
MIT许可证。