Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check, swift version for Playground?

I was just testing my code in play ground(xcode-8.2), using swift tutorial. I came across following sample code:

One-Sided Ranges

for name in names[2...] {
    print(name)
}

now my play ground showing an error:

enter image description here

now I feel that my swift version may not be supporting this code!

I looked around this answer but it provide solution for Xcode Project only.

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

How can I see swift version of play ground?

like image 323
Gaurang Chokhariya Avatar asked Mar 14 '18 05:03

Gaurang Chokhariya


People also ask

How do I change the Swift code in Xcode playground?

Xcode can use the different version of the Swift using the build setting $SWIFT_VERSION from the build setting tab of Xcode target if we have other Swift versions available.

Does Xcode still have playground?

To open Playground on Xcode, navigate to File in the menu and click on New > Playground... To test our square function, we will choose the Blank template. Name your Playground file, then click on Create.

What is the difference between Swift playground and Xcode?

One of the biggest differences between Swift Playgrounds and Xcode Playgrounds is that Swift Playgrounds are much less powerful and are built more as an educational tool. My biggest fear is that as Apple brings Swift Playgrounds to the Mac that they will stop supporting and growing Xcode Playgrounds.


1 Answers

Try to find out swift version using following code. (Here I tried this code with Playground of Xcode 9.3 - beta 4) and it's providing me correct answer.

#if 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.1)
print("Hello, Swift 4.1")
#elseif swift(>=4.0)
print("Hello, Swift 4.0")
#elseif swift(>=3.0)
print("Hello, Swift 3.x")
#else
print("Hello, Swift 2.2")
#endif

enter image description here

Answer to your question: I'm not sure but according to result of above code, I can say, Latest Swift version supported by your Xcode tool becomes a version of Playground's Swift Language.

like image 92
Krunal Avatar answered Sep 18 '22 12:09

Krunal