版本

CI Status

Version 是一个 Swift 库,它能够表示和比较语义版本号。它遵循 语义版本控制 2.0.0

表示方式是

用法

版本可以直接实例化

let version = Version(
    major: 1,
    minor: 2,
    patch: 3,
    prerelease: "alpha.1",
    build: "B001"
)

或者它们可以从字符串表示形式转换而来

let version = try Version("1.2.3-alpha.1+B001")

let version: Version = "1.2.3-alpha.1+B001"
// The line above is equivalent to:
let version = try! Version("1.2.3-alpha.1+B001")

版本之间可以相互比较

let version = Version(from: ProcessInfo.processInfo.operatingSystemVersion)

if version > "8.0.0" {
    // do something in a more amazing way
} else if version > "7.0.0"
    // do it an old-fashioned, legacy-style
} else {
    // do not care …
}

除了 UIKit 的 UIDevice,还支持在 Foundation 中访问操作系统版本的更佳方式,如下所示。

let version = Version(from: ProcessInfo.processInfo.operatingSystemVersion)
if version == "8.0.1" {
    NSLog("Sorry no cellular data for you, my friend!")
}

最后,版本可以直接从 bundle 中读取

if NSBundle(path: "Alamofire.framework").version! < "1.0.0" {
    println("Howdy Ho! Such an early-adopter using an unstable version!")
    println("Beware: “Anything may change at any time.”")

    // … or insert an actual meaningful special handling
    // for version-specific *implementation details* here.
}

注意:在检查框架版本时要小心。此类检查发生在运行时。如果在错误的地方使用,它们会损害性能。如果 API 发生更改并且您想使用新方法,则必须在编译时通过使用预编译器宏 (#if) 检查已传递给编译器构建设置 OTHER_SWIFT_FLAGS 的定义来完成。

安装

Swift 包管理器

.package(url: "https://github.com/mrackwitz/Version.git", ),

Cocoapods

Version 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile

use_frameworks!
pod 'Version'

Carthage

github "mrackwitz/Version"

作者

Marius Rackwitz, git@mariusrackwitz.de
在 Twitter 上找到我 @mrackwitz

许可证

Version 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。