5

I am limited to Xcode 6.2 if I am using Mavericks, and the following code:

let setOfNumbers: Set<Int> = [1, 2, 3, 4, 5];

won't work. So Set should only work in Swift 1.2 and above. How do I print out what Swift version I am using in the program? (just like p RUBY_VERSION if using Ruby)

pkamb
  • 33,281
  • 23
  • 160
  • 191
nonopolarity
  • 146,324
  • 131
  • 460
  • 740

2 Answers2

7

Updated answer

Starting with Swift 2.2 (Xcode 7.3b) you can determine the Swift version and run code conditionnally with the #if swift() build configuration.

Example:

#if swift(>=2.2)
    print("Running Swift 2.2 or later")
#else
    print("Running Swift 2.1 or earlier")
#endif

The branch containing code for the compatible version of Swift will be executed, and the other branch will be ignored.

Old answer

There's no known way to do it in code (ref, ref).

You can do this in the Terminal:

swift -version

For this command to be accurate you need to check that Xcode tools are linked to the proper Xcode version (can be confusing if you have Xcode and Xcode-beta installed side by side).

Community
  • 1
  • 1
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • your 2nd ref did it... `swift -version` won't work as I probably need to set up or install some command line tools first – nonopolarity Aug 31 '15 at 13:54
4

This is the way you can check in Terminal:

$ xcrun swift -version

It will gives you result.

Murali
  • 1,084
  • 1
  • 11
  • 28
  • yup, it is giving me 2 lines: `Swift version 1.1 (swift-600.0.57.4) Target: x86_64-apple-darwin13.4.0` – nonopolarity Aug 31 '15 at 13:42
  • on my Macbook 12 inch with Yosemite and XCode 6.4, it is giving me `Apple Swift version 1.2 (swiftlang-602.0.53.1 clang-602.0.53) Target: x86_64-apple-darwin14.5.0` – nonopolarity Aug 31 '15 at 14:12