Swiftpack.co - Package - sersoft-gmbh/SemVer


GitHub release Tests Codacy Badge codecov jazzy

This repository contains a complete implementation of a Version struct that conforms to the rules of semantic versioning which are described at semver.org.


Add the following dependency to your Package.swift:

.package(url: "https://github.com/sersoft-gmbh/semver.git", from: "3.0.0"),


  • For Swift up to version 5.2, use SemVer version 2.x.y.
  • For Swift as of version 5.3, use SemVer version 3.x.y.


Creating Versions

You can create a version like this:

let version = Version(major: 1, minor: 2, patch: 3,
                      prerelease: "beta",
                      metadata: "exp", "test") // metadata could also be ["exp, test"]
version.versionString() // -> "1.2.3-beta+exp.test"

Of course there are simpler ways:

let initialRelease = Version(major: 1)
initialRelease.versionString() // -> "1.0.0"

let minorRelease = Version(major: 2, minor: 1)
minorRelease.versionString() // -> "2.1.0"

let patchRelease = Version(major: 3, minor: 2, patch: 1)
patchRelease.versionString() // -> "3.2.1"

Version Strings

As seen in above's examples, there's a func to return a string represenation of a Version. The versionString(formattedWith options: FormattingOptions = default) function allows to retrieve a formatted string using the options passed. By default the full version is returned. The following options currently exist:

  • .dropPatchIfZero: If patch is 0, it won't be added to the version string.
  • .dropMinorIfZero: If minor and patch are both 0, only the major number is added. Requires .dropPatchIfZero.
  • .dropTrailingZeros: A convenience combination of .dropPatchIfZero and .dropMinorIfZero.
  • .includePrerelease: If prerelease is not empty, it is added to the version string.
  • .includeMetadata: If metadata is not empty, it is added to the version string.
  • .fullVersion: A convenience combination of .includePrerelease and .includeMetadata. The default if you don't pass anything to versionString.
let version = Version(major: 1, minor: 2, patch: 3,
                      prerelease: "beta",
                      metadata: "exp", "test")
version.versionString(formattedWith: .includePrerelease]) // -> "1.2.3-beta"
version.versionString(formattedWith: .includeMetadata) // -> "1.2.3+exp.test"
version.versionString(formattedWith: []) // -> "1.2.3"

let version2 = Version(major: 2)
version2.versionString(formattedWith: .dropPatchIfZero) // -> "2.0"
version2.versionString(formattedWith: .dropTrailingZeros) // -> "2"

A Version can also be created from a String. All Strings created by the versionString func should result in the same Version they were created from:

let version = Version(major: 1, minor: 2, patch: 3,
                      prerelease: "beta",
                      metadata: "exp", "test")
let str = version.versionString() // -> "1.2.3-beta+exp.test"
let recreatedVersion = Version(str) // recreatedVersion is Optional<Version>
recreatedVersion == version // -> true

Comparing Versions

A Version can also be compared to other versions. This also follows the rules of semantic versioning. This means that metadata has no effect on comparing at all. This also means that a version with and without metadata are treated as equal:

let versionWithMetadata = Version(major: 1, minor: 2, patch: 3,
                                  metadata: "exp", "test")
let versionWithoutMetadata = Version(major: 1, minor: 2, patch: 3)
versionWithMetadata == versionWithoutMetadata // -> true

Otherwise, comparing two Version's basically compares their major/minor/patch numbers. A Version with a prerelease is ordered before a the same version without prerelease:

let prereleaseVersion = Version(major: 1, minor: 2, patch: 3,
                                prerelease: "beta")
let finalVersion = Version(major: 1, minor: 2, patch: 3)
prereleaseVersion < finalVersion // -> true

Validity Checks

Version performs some validity checks on its fields. This means, that no negative numbers are allowed for major, minor and patch. Also, the prerelease and metadata Strings must only contain alphanumeric characters plus - (hyphen). However, to keep working with Version production-safe, these rules are only checked in non-optimized builds (using assert()). The result of using not allowed numbers / characters in optimized builds is undetermined. While calling versionString() very likely won't break, it certainly won't be possible to recreate a version containing invalid numbers / characters using init(_ description: String).


The API is documented using header doc. If you prefer to view the documentation as a webpage, there is an online version available for you.


If you find a bug / like to see a new feature in SemVer there are a few ways of helping out:

  • If you can fix the bug / implement the feature yourself please do and open a PR!
  • If you know how to code (which you probably do), please add a (failing) test and open a PR. We'll try to get your test green ASAP.
  • If you can't do neither, then open an issue. While this might be the easiest way, it will likely take the longest for the bug to be fixed / feature to be implemented.


See LICENSE file.


Stars: 1


Used By

Total: 0


v3.0.0 RC 1 - 2020-08-26 15:33:36

This is the first release candidate for version 3.0.0. The minor version increase is due to dropping support for Swift pre 5.3 as well as removing older, deprecated APIs.

v2.3.1 - 2020-03-25 12:49:36

Addes support for Swift 5.2

v2.3.0 - 2019-09-26 13:08:53

This introduces Swift 5.1 support while maintaining Swift 5.0 support. No functional changes were made.

v2.2.2 - 2019-07-28 18:22:31

  • Extend Travis configuration
  • Add documentation

v2.2.1 - 2019-03-26 08:03:51

Also update Travis to Swift 5

v2.2.0 - 2019-03-26 07:43:40

This adds support for Swift 5 (backwards compatible with Swift 4.2)

v2.1.1 - 2019-01-18 17:52:25

Slight project restructuring to enable CodeCov

v2.1.0 - 2019-01-03 14:52:34

This adds a standalone Xcode project (in the Xcode subfolder) for integration with "normal" Xcode projects (outside of the Swift Package Manager)

v2.0.0 - 2018-08-29 14:51:53

This release updates to Swift 4.2 and adds Travis CI integration.

Version 1.1.0 - 2017-12-29 14:52:57

New way of formatting Versions. Allow missing minor/patch in formatted strings and input strings.

Version 1.0.1 - 2017-12-27 13:10:27

Swift 4 update

1.0.0 - 2017-06-02 08:18:38

The initial version