Represent and compare versions via semantic versioning (SemVer) in Swift

Overview

Version

CI Status

Version is a Swift Library, which enables to represent and compare semantic version numbers. It follows Semantic Versioning 2.0.0.

The representation is:

  • Comparable
  • Hashable & Equatable
  • String Literal Convertible
  • Printable

Usage

Versions could be either instantiated directly:

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

Or they can be converted from a string representation:

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")

Versions can be compared between each other:

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 …
}

Besides UIKit's UIDevice the more preferable variant to access the operation system version in Foundation as shown below is supported, too.

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

Finally Versions can be directly read from bundles:

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.
}

ATTENTION: Take care when you check versions of frameworks. Such checks happen at runtime. They can hurt performance if used at the wrong place. If there are API changes and you want to consume new methods, you have to do that at compile time by checking with precompiler macros (#if) for definitions, which have been passed to the compiler build setting OTHER_SWIFT_FLAGS.

Installation

Swift Package Manager

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

Cocoapods

Version is available through CocoaPods. To install it, simply add the following lines to your Podfile:

use_frameworks!
pod 'Version'

Carthage

github "mrackwitz/Version"

Author

Marius Rackwitz, [email protected]
Find me on Twitter as @mrackwitz.

License

Version is available under the MIT license. See the LICENSE file for more info.

Comments
  • Bundle.main.version shows build number

    Bundle.main.version shows build number

    Bundle.main.version shows the build number with kCFBundleVersionKey when it should show Bundle.main.shortVersion + "-" + Bundle.main.version

    Proposing version be renamed to build

    Let me know if my project is just wrong but I think my understanding of the keys is correct.

    opened by twodayslate 4
  • Fails to build with Carthage

    Fails to build with Carthage

    Judging by #14 this used to work with Carthage once upon a time, but currently fails. Would love to see it working again! 😍

    Last line of log file:

    The following build commands failed: ProcessInfoPlistFile /Users/ianbytchek/Library/Caches/org.carthage.CarthageKit/DerivedData/9.2_9C40b/version/0.6.1/Build/Intermediates.noindex/ArchiveIntermediates/Version/IntermediateBuildFilesPath/UninstalledProducts/macosx/Version.framework/Versions/A/Resources/Info.plist Version/Info.plist

    opened by iby 3
  • Examples in README don't seem to work

    Examples in README don't seem to work

    The README suggests that I ought to be able to do

    let version : Version = UIDevice.currentDevice().systemVersion
    

    but when I do this, I get the following error: "Cannot convert value of type 'String' to specified type 'Version'".

    Googling around, it seems like the code shouldn't be expected to work?

    ExpressibleByStringLiteral… is only involved when dealing with literals… you’re dealing with a string value.

    Indeed, when I use a literal instead, like

    let version : Version = "13.4"
    

    then it does work.

    opened by wearhere 2
  • Initialize a version from its string representation should be Optional

    Initialize a version from its string representation should be Optional

    Version("") will crash the device. Shouldn't it be init? instead of init!

    Error: Failed to parse version number '': InvalidComponents
    Fatal error: Unexpectedly found nil while unwrapping an Optional value
    2019-09-11 21:05:13.709726-0400 ec3730[79029:21121642] Fatal error: Unexpectedly found nil while unwrapping an Optional value
    
    opened by twodayslate 2
  • Extend Version to conform to the Codable protocol

    Extend Version to conform to the Codable protocol

    I originally wrote a comment about not being sure how to do this because I was coding the string representations. Now the object is being fully and properly coded.

    opened by guykogus 2
  • Error on extension UIDevice

    Error on extension UIDevice

    I'm trying to use Version (0.4.0) on Xcode 7.3.1 but when I compile my project it gives me an issue:

    Version.swift:271:33: Cannot convert value of type 'Version?' to expected argument type 'String'

    opened by baquiax 2
  • Fix conflicts with semver specifications and several bugs.

    Fix conflicts with semver specifications and several bugs.

    Fixes problems described in issue #10.

    :zap: CAUTION: This is a breaking change! :bomb:

    What I changed/fixed:

    1. In order to allow Version to leniently parse invalid (according to semver, at least), yet common versions (and print them back identically to how they were parsed) I added Version.canonicalMinor and Version.canonicalPatch for internal handling.
    public var canonicalMinor: Int {
        return self.minor ?? 0
    }
    
    1. I extracted the parsing code into a dedicated VersionParser class, which can now be initialized with a strict flag (false being the default). Again, we don't want to break parsing of semi-valid version strings.
    2. This is a big one. In order to be a valid semantic version it should ignore build in comparisons. Ignoring build would however lead to unexpected results when comparing “1.0.0+A” and “1.0.0+B”. One would expect these to not be equal. Then again as long as one cannot guarantee a comparable build number (it might be a partial git commit hash, for which sorting makes no sense whatsoever) I therefor removed comparison of build from “func<“, "func==“ and “hashValue” and implemented func=== to re-enable equality checks with build:
    public func ===(lhs: Version, rhs: Version) -> Bool {
        return (lhs == rhs) && (lhs.build == rhs.build)
    }
    
    1. Fixed.
    2. A nil-value in minor or patch now is considered equal to “0”.
    opened by regexident 2
  • EXC_BAD_INSTRUCTION when comparing

    EXC_BAD_INSTRUCTION when comparing

    I keep getting this crash when comparing semantic versions with your latest CocoaPods release 0.2.0.

    Even on the command line it crashes Xcode when doing e.g.:

    p NSBundle.mainBundle().shortVersion! < "1.0.0"
    

    Any idea what's going on? I'm using Xcode Version 6.1.1 (6A2008a).

    opened by MattesGroeger 2
  • Fix compilation issue on Linux

    Fix compilation issue on Linux

    Fix this issue on Linux stack.

    remote: /tmp/build_90a63a7a429caba3f86535bd8a819ae1/.build/checkouts/Version/Sources/Version/Version.swift:258:63: error: use of unresolved identifier 'kCFBundleVersionKey'
    remote:         return self.versionFromInfoDicitionary(forKey: String(kCFBundleVersionKey))
    
    opened by mackoj 1
  • Upgrade project for Xcode 10.2, Swift 5

    Upgrade project for Xcode 10.2, Swift 5

    I upgraded the Hashable implementation, while leaving it backwards-compatible. I deleted all the code for Codable since it's auto-synthesised. Will that make it non-backwards-compatible? 🤔

    opened by guykogus 1
  • Swift 2.3 Support & some build issue fixes

    Swift 2.3 Support & some build issue fixes

    0.5.0 does not build with Xcode 8. No changes for source needed, just some updates of xcodeproj & build schemes.

    Also fixed a build issue cause by incorrect path of Info.plist for Release configuration.

    Adjust deployment targets: macOS: 10.9 iOS: 8.0 watchOS: 2.0 tvOS: 9.0

    opened by cezheng 1
  • Carthage is failing to bootstrap and update 0.7.2

    Carthage is failing to bootstrap and update 0.7.2

    Version 0.7.1 works perfectly, Carthage/Carthage/issues/1763 might be related.

    *** Checking out Version at "0.7.2"
    Parse error: expected submodule commit SHA in output of task (ls-tree -z 0.7.2 xcconfigs) but encountered: 
    
    opened by iby 3
Owner
Marius Rackwitz
Cofounder @nlbb, Developer, Swift Pioneer @CocoaPods Core team, Conference Speaker. Email me to: contact@$firstName$lastName.de
Marius Rackwitz
Notify users when a new version of your app is available and prompt them to upgrade.

Siren ?? Notify users when a new version of your app is available and prompt them to upgrade. Table of Contents Meta About Features Screenshots Ports

Arthur Ariel Sabintsev 4.1k Dec 27, 2022
A git plugin for real-world xcode versioning workflow.

git-xcp The most simplest, safe, and fully automatic git plugin for versioning workflow of real-world xcode projects. Current working or draft content

gitmerge 11 Dec 29, 2019
Conv smart represent UICollectionView data structure more than UIKit.

Conv Conv smart represent UICollectionView data structure more than UIKit. Easy definition for UICollectionView DataSource and Delegate methods. And C

bannzai 157 Nov 25, 2022
Easier way to represent the structure of UITableView.

Shoyu Shoyu is a library written in Swift to represent UITableView data structures. Shoyu means Soy Sauce in Japanese. Usage Create single section and

yukiasai 278 Apr 14, 2022
A way to represent what you’re sharing.

About This project provides a preview of items being shared via UIActivityViewController. Example: // standard activity view controller let vc = UIAct

Ryan Ackermann 759 Nov 24, 2022
Conv smart represent UICollectionView data structure more than UIKit.

Conv Conv smart represent UICollectionView data structure more than UIKit. Easy definition for UICollectionView DataSource and Delegate methods. And C

bannzai 155 May 12, 2022
A simple deep learning library for estimating a set of tags and extracting semantic feature vectors from given illustrations.

Illustration2Vec illustration2vec (i2v) is a simple library for estimating a set of tags and extracting semantic feature vectors from given illustrati

Masaki Saito 661 Dec 12, 2022
Graph is a semantic database that is used to create data-driven applications.

Welcome to Graph Graph is a semantic database that is used to create data-driven applications. Download the latest sample. Features iCloud Support Mul

Cosmicmind 875 Oct 5, 2022
Mobile Text-to-Image search powered by multimodal semantic representation models(e.g., OpenAI's CLIP)

A Mobile Text-to-Image Search Powered by AI A minimal demo demonstrating semantic multimodal text-to-image search using pretrained vision-language mod

null 66 Jan 5, 2023
Mobile(iOS) Text-to-Image search powered by multimodal semantic representation models(e.g., OpenAI's CLIP)

Mobile Text-to-Image Search(MoTIS) MoTIS is a minimal demo demonstrating semantic multimodal text-to-image search using pretrained vision-language mod

Roy 66 Dec 2, 2022
The example project of inferencing Semantic Segementation using Core ML

SemanticSegmentation-CoreML This project is Object Segmentation on iOS with Core ML. If you are interested in iOS + Machine Learning, visit here you c

tucan9389 248 Dec 17, 2022
SwiftDate 🐔 Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift.

Toolkit to parse, validate, manipulate, compare and display dates, time & timezones in Swift. What's This? SwiftDate is the definitive toolchain to ma

Daniele Margutti 7.2k Jan 4, 2023
Units is a Swift package to manipulate, compare, and convert between physical quantities.

Units ?? Units is a Swift package to manipulate, compare, and convert between physical quantities. This package models measurements, which are a numer

Jay Herron 3 Jun 22, 2022
Compare your implementation and design, see how much they match!

DesignDetective Preview Usage You may trigger anywhere, but I personally prefer using it via Shake Gesture import DesignDetective extension UIWindow

Enes Karaosman 43 Dec 13, 2022
To compare the pictures and detect a similar face.

AWS Face Reckognition To compare the pictures and detect a similar face. Project Created & Maintained By MultiQoS Pvt. Ltd. Demo Screen.Recording.2022

MultiQoS 24 Sep 16, 2022
Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm

Differific is a diffing tool that helps you compare Hashable objects using the Paul Heckel's diffing algorithm. Creating a chan

Christoffer Winterkvist 127 Jun 3, 2022
Async and concurrent versions of Swift’s forEach, map, flatMap, and compactMap APIs.

CollectionConcurrencyKit Welcome to CollectionConcurrencyKit, a lightweight Swift package that adds asynchronous and concurrent versions of the standa

John Sundell 684 Jan 9, 2023
null 13 Oct 28, 2022
Easy way to detect iOS device properties, OS versions and work with screen sizes. Powered by Swift.

Easy way to detect device environment: Device model and version Screen resolution Interface orientation iOS version Battery state Environment Helps to

Anatoliy Voropay 582 Dec 25, 2022
swiftenv allows you to easily install, and switch between multiple versions of Swift.

Swift Version Manager swiftenv allows you to easily install, and switch between multiple versions of Swift. This project was heavily inspired by pyenv

Kyle Fuller 1.9k Dec 27, 2022