How to Programmatically Find Swift's Version

How can I programmatically find Swift's version?

Swift 3.1 extends the @available attribute to support specifying Swift version numbers in addition to its existing platform versions.

// Swift 3.1

@available(swift 3.1)
func intVersion(number: Double) -> Int? {
return Int(exactly: number)
}

@available(swift, introduced: 3.0, obsoleted: 3.1)
func intVersion(number: Double) -> Int {
return Int(number)
}

How do I see which version of Swift I'm using?

Project build settings have a block 'Swift Compiler - Languages', which stores information about Swift Language Version in key-value format. It will show you all available (supported) Swift Language Version for your Xcode and active version also by a tick mark.

Project ► (Select Your Project Target) ► Build Settings ► (Type
'swift_version' in the Search bar) Swift Compiler Language ► Swift Language
Version ► Click on Language list to open it (and there will be a tick mark on any one of list-item, that will be current swift version).

Look at this snapshot, for easy understanding:

xcode with described areas highlighted


With help of following code, programmatically you can find Swift version supported by your project.

#if swift(>=5.7)
print("Hello, Swift 5.7")

#elseif swift(>=5.6)
print("Hello, Swift 5.6")

#elseif swift(>=5.5)
print("Hello, Swift 5.5")

#elseif swift(>=5.4)
print("Hello, Swift 5.4")

#elseif swift(>=5.3)
print("Hello, Swift 5.3")

#elseif swift(>=5.2)
print("Hello, Swift 5.2")

#elseif swift(>=5.1)
print("Hello, Swift 5.1")

#elseif swift(>=5.0)
print("Hello, Swift 5.0")

#elseif swift(>=4.2)
print("Hello, Swift 4.2")

#elseif swift(>=4.1)
print("Hello, Swift 4.1")

#elseif swift(>=4.0)
print("Hello, Swift 4.0")

#elseif swift(>=3.2)
print("Hello, Swift 3.2")

#elseif swift(>=3.0)
print("Hello, Swift 3.0")

#elseif swift(>=2.2)
print("Hello, Swift 2.2")

#elseif swift(>=2.1)
print("Hello, Swift 2.1")

#elseif swift(>=2.0)
print("Hello, Swift 2.0")

#elseif swift(>=1.2)
print("Hello, Swift 1.2")

#elseif swift(>=1.1)
print("Hello, Swift 1.1")

#elseif swift(>=1.0)
print("Hello, Swift 1.0")

#endif

Here is result using Playground (with Xcode 11.x)

Sample Image

How can I determine the latest iOS version programmatically using Swift?

Apple provides an API at curl http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/com.apple.jingle.appserver.client.MZITunesClientCheck/version. Test it on the command line using:

curl http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wa/com.apple.jingle.appserver.client.MZITunesClientCheck/version

The response is XML, and the information you need is keyed by device version (eg iPhone8,1).

You can access your device's version by using sysctlbyname() from Swift.

Update Looks like the old URL (http://phobos.apple.com./version) died a while back (thanks Troy) so have updated my answer with another version. I have no idea if the content is the same.

How to print out Swift's version in a program?

This is the way you can check in Terminal:

$ xcrun swift -version

It will gives you result.

Check OS version in Swift?

For iOS, try:

var systemVersion = UIDevice.current.systemVersion

For OS X, try:

var systemVersion = NSProcessInfo.processInfo().operatingSystemVersion

If you just want to check if the users is running at least a specific version, you can also use the following Swift 2 feature which works on iOS and OS X:

if #available(iOS 9.0, *) {
// use the feature only available in iOS 9
// for ex. UIStackView
} else {
// or use some work around
}

BUT it is not recommended to check the OS version. It is better to check if the feature you want to use is available on the device than comparing version numbers.
For iOS, as mentioned above, you should check if it responds to a selector;
eg.:

if (self.respondsToSelector(Selector("showViewController"))) {
self.showViewController(vc, sender: self)
} else {
// some work around
}

Getting version and build information with Swift

What was wrong with the Swift syntax? This seems to work:

if let text = Bundle.main.infoDictionary?["CFBundleVersion"] as? String {
print(text)
}

How do I get the App version and build number using Swift?

EDIT

Updated for Swift 4.2

let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String

EDIT

As pointed out by @azdev on the new version of Xcode you will get a compile error for trying my previous solution, to solve this just edit it as suggested to unwrap the bundle dictionary using a !

let nsObject: AnyObject? = Bundle.main.infoDictionary!["CFBundleShortVersionString"]

End Edit

Just use the same logic than in Objective-C but with some small changes

//First get the nsObject by defining as an optional anyObject
let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary["CFBundleShortVersionString"]

//Then just cast the object as a String, but be careful, you may want to double check for nil
let version = nsObject as! String

Product version number in swift code

In Swift 5.3:

extension UIApplication {

static var appVersion: String? {
return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
}

}

Usage:

UIApplication.appVersion!


Related Topics



Leave a reply



Submit