Semantic Versioning implementation in Swift!
Use the struct Version
to represent a version according to the Semantic Versioning Specification 2.0.0.
✅ Fully Unit tested
✅ 100% Swift
- iOS 8.0+ / Mac OS X 10.9+
- Xcode 10.0+
- Swift 4.2
The easiest way to use SemanticVersion in your project is using the CocaPods package manager.
See installation instructions for CocoaPods if not already installed
To integrate the library into your Xcode project specify the pod dependency to your Podfile
:
platform :ios, '8.0'
use_frameworks!
pod 'SemanticVersioning'
run pod install
pod install
Create version 2.0.0
let version = Version(major: 2)
Create version 1.2.3
let version = Version(major: 1, minor: 2, patch: 3)
Create version 1.0.0-alpha.2
let version = Version(major: 1, preReleaseIdentifier: ["alpha", "2"])
Create version from a String
let version: Version = "1.3.10-rc"
Create a list of versions from a Array of Strings
let versions: [Version] = ["1.0.0-alpha", "1.0.0-alpha.1"]
Check if is prerelease version or not
if version.isPrerelease { ... }
Access the prerelease identifier via the preReleaseIdentifier Array
for identifier in version.preReleaseIdentifier
{
// ...
}
Access the build metadata identifier via the buildMetadataIdentifier Array
for identifier in version.buildMetadataIdentifier
{
// ...
}
Conforms to Printable so you can simply get a String representation by accessing the description property
println(version)
// OR
let stringRepresentation = version.description
mutability / immutability
The default operators for comparsion are implemented
<
, <=
, >
,>=
,==
, !=
This will comapre major, minor, patch and the prerelease identifiers according to the Semantic Versioning Sepcification 2.0.0
The implementation includes a full-fledged component ot parse String representation of a version. Please have a look at the tests and the soruce of SemanticVersioningParser
for now 😉
The libary includes a suite of tests showing how to use the different initialiser and the Parser
There are some build in extensions to make your live easier.
NSOperatingSystemVersion
conforms to SemanticVersion
so it can be compared with all other structs / objects that conform to the same protocol (e.g. Version
).
So you can check the current system Version like this (on iOS 8+):
let systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
if systemVersion < Version(major: 8)
{
// ...
}
You can get the version as struct Version
of a NSBundle if set in the bunlde info.plist like:
let bundle = NSBundle(forClass: self.dynamicType)
if let version = bundle.version
{
// ...
}
You can get the operating system version from a UIDevice
object by using operatingSystemVersion
which returns a Version
representation of systemVersion
.
let systemVersion = UIDevice.currentDevice().operatingSystemVersion
if systemVersion < Version(major: 8)
{
// ...
}
Converts an Integer to a Version
struct. You can use this only to represent major versions.
let systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
if systemVersion < 8
{
// ...
}
Reducing the version's minor and major value to a Float
is also possible. Use this with Float
representation could lead to accuracy loss of the minor value (that is represented as fractional digits). I would not reccommend to compare with ==
but <
, <=
, >
,>=
should be useful.
let systemVersion = NSProcessInfo.processInfo().operatingSystemVersion
if systemVersion.floatValue() < 8.1
{
// ...
}
Create your own extensions or Version representations by creating struct / object that conforms to SemanticVersioning
. Have a look at the extensions or the Version
implementation for more information.