Version
public struct Version : Sendable, SemanticVersionComparable
extension Version: LosslessStringConvertible
extension Version: ExpressibleByStringLiteral
extension Version: ExpressibleByStringInterpolation
extension Version: CustomDebugStringConvertible
A version type conforming to SemanticVersionComparable
and therefor SemVer
.
You can create a new version using strings, string literals and string interpolations, formatted
like MAJOR.MINOR.PATCH-PRERELEASE+BUILD
, or memberwise initialization.
// from string
let version: Version? = "1.0.0"
let version: Version? = Version("1.0.0-alpha.1+23")
let version: Version? = Version("1.0.0.1.2") // <- will be `nil` since it's not `SemVer`
let version: Version = "1.0.0" // <- will crash if string does not conform to `SemVer`
// from memberwise properties
let version: Version = Version(1, 0, 0)
let version: Version = Version(major: 1, minor: 0, patch: 0, prerelease: ["alpha, "1"], build: ["exp"])
Pre-release identifiers or build-meta-data can be handled as strings or as enum cases with it associated raw
values (see PrereleaseIdentifier
and BuildMetaData
for more).
let version: Version = Version(major: 1, minor: 0, patch: 0, prerelease: ["alpha"], build: ["500"])
version.absoluteString // -> "1.0.0-alpha+500"
let version: Version = Version(2, 32, 16, ["family", .alpha], ["1"])
version.absoluteString // -> "2.32.16-family.alpha+1"
version.coreString // -> "2.32.16"
version.extensionString // -> "family.alpha+1"
version.prereleaseIdentifer // -> "family.alpha"
version.buildMetaDataString // -> "1"
Remark
See semver.org for detailed information.-
Declaration
Swift
public var major: UInt
-
Declaration
Swift
public var minor: UInt?
-
Declaration
Swift
public var patch: UInt?
-
Declaration
Swift
public var prerelease: [PrereleaseIdentifier]?
-
Declaration
Swift
public var build: [BuildMetaData]?
-
Creates a new version.
Note
Unsigned integers are used to provide an straightforward way to make sure that the identifiers are not negative numbers.
Declaration
Swift
@inlinable public init( _ major: UInt, _ minor: UInt? = nil, _ patch: UInt? = nil, _ prerelease: [PrereleaseIdentifier]? = nil, _ build: [BuildMetaData]? = nil )
Parameters
major
The
MAJOR
identifier of a version.minor
The
MINOR
identifier of a version.patch
The
PATCH
identifier of a version.prerelease
The pre-release identifier of a version.
build
The build-meta-data of a version.
Return Value
A new version.
-
Creates a new version.
Note
Unsigned integers are used to provide an straightforward way to make sure that the identifiers are not negative numbers.
Declaration
Swift
@inlinable public init( major: UInt, minor: UInt? = nil, patch: UInt? = nil, prerelease: [PrereleaseIdentifier]? = nil, build: [BuildMetaData]? = nil )
Parameters
major
The
MAJOR
identifier of a version.minor
The
MINOR
identifier of a version.patch
The
PATCH
identifier of a version.prerelease
The pre-release identifier of a version.
build
The build-meta-data of a version.
-
Creates a new version using a string.
Declaration
Swift
public init?(private string: String)
Parameters
string
The string representing a version.
-
Declaration
Swift
public var description: String { get }
-
Creates a new version from a string.
Declaration
Swift
public init?(_ string: String)
Parameters
string
A string beeing parsed into a version.
Return Value
A version object or
nil
if string does not conform toSemVer
.
-
Creates a new version from a string literal.
Warning
Usage is not recommended unless the given string conforms toSemVer
.Declaration
Swift
public init(stringLiteral value: StringLiteralType)
-
Creates a new version from a string interpolation.
Warning
Usage is not recommended unless the given string conforms toSemVer
.Declaration
Swift
public init(stringInterpolation: DefaultStringInterpolation)
-
An initial version representing the string
0.0.0
.Declaration
Swift
static var initial: Version
-
Declaration
Swift
public var debugDescription: String { get }