How Use and Run Swift 2.3 on Command Line

How can I use swift in Terminal?


sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

then you can do one of these:

xcrun swift 
lldb --repl

As of Xcode 6.1 - typing swift in the terminal launches the REPL as well.

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

Where are the swift/swiftc binaries for v3?

Thanks to Marc B's suggestion:

⇒  find / -name swift -type f
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
/Users/myusername/Desktop/Xcode-beta.app/Contents/Developer/Toolchains/Swift_2.3.xctoolchain/usr/bin/swift
/Users/myusername/Desktop/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
/usr/bin/swift

Which for me are:

⇒  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift --version
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9

⇒ /Users/myusername/Desktop/Xcode-beta.app/Contents/Developer/Toolchains/Swift_2.3.xctoolchain/usr/bin/swift --version
Apple Swift version 2.3 (swiftlang-800.10.6 clang-800.0.24.1)
Target: x86_64-apple-macosx10.9

⇒ /Users/myusername/Desktop/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift --version
Apple Swift version 3.0 (swiftlang-800.0.30 clang-800.0.24)
Target: x86_64-apple-macosx10.9

⇒ /usr/bin/swift --version
Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9

To set v3 as your 'main' swift, you can do the following (make sure to use the right path to your Xcode 8 application):

⇒  sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer

⇒ xcrun --find swift
/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

⇒ swift --version
Apple Swift version 3.0 (swiftlang-800.0.30 clang-800.0.24)
Target: x86_64-apple-macosx10.9

Have anyone seen, warning: target specifies SWIFT_VERSION = '2.3'

It looks like the some of your dependencies may be specifying swift 2.3. So it is warning you that this may cause problems in the next upcoming update. You also should check the setting for your project and make sure under project settings that the Use Legacy Swift Language Version' = NO.



Related Topics



Leave a reply



Submit