Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically find Swift's version?

I know I can find the version of Swift I'm running right now reverting to a Terminal and typing:

xcrun swift --version Swift version 1.1 (swift-600.0.57.4) Target: x86_64-apple-darwin13.4.0 

Also, I've been reading about the Preprocessor Macros in Swift, but no luck finding a Swift version constant.

As Swift 1.2 approaches it will be nice to flag old code that only runs on Swift 1.1 (Xcode up to 6.2) or new code that needs Xcode 6.3 (Swift 1.2)

Note: I can also use system() to do something like:

system("xcrun swift --version | grep version > somefile.txt") 

Then open somefile.txt, but rather prefer some simpler solution

like image 443
Diego Freniche Avatar asked Mar 19 '15 09:03

Diego Freniche


People also ask

What is the version of Swift?

Swift 5.1 was officially released in September 2019.

What version of Swift does Xcode use?

This book describes Swift 5.7, the default version of Swift that's included in Xcode 14. You can use Xcode 14 to build targets that are written in either Swift 5.7, Swift 4.2, or Swift 4.


1 Answers

You can use conditional compilation directives to test for the specific Swift version used to build your project:

#if swift(>=5.0) print("Hello, Swift 5!") #elseif swift(>=4.0) print("Hello, Swift 4!") #elseif swift(>=3.0) print("Hello, Swift 3!") #elseif swift(>=2.2) print("Hello, Swift 2.2!") #elseif swift(>=2.1) print("Hello, Swift 2.1!") #endif 
like image 152
crishoj Avatar answered Oct 11 '22 00:10

crishoj