16

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 can I see swift version of play ground?

5
  • Xcode 9 - Swift 4. Xcode 8 - Swift 3. Commented Mar 14, 2018 at 5:44
  • @rmaddy - It seems, a question for me, also, Xcode 8.2 supports Swift 2.0 to 3.2 (approx). How can we ensure, playground is supporting 3.0 and not other. How can I check it. Commented Mar 14, 2018 at 5:48
  • 2
    @Krunal It's been too long since I've used Xcode 8 and I never used Swift 2. But the APIs are radically different between Swift 2 and 3. Try using any Swift 3 syntax. Better yet, get current. Use Xcode 9.2 and use Swift 4. Nobody should be using Swift 2 any more. it's history. Commented Mar 14, 2018 at 5:52
  • @rmaddy Yes, I also use Xcode 9.2+, a simple question is, an Xcode tools supports multiple versions of Swift language. How can we ensure, which specific version of swift languages is supported by a playground. Does it supports a latest version of that particular Xcode tool, by default? Commented Mar 14, 2018 at 5:55
  • Apple documentation is currently for Swift 4. names[2...] is Swift 4. Only Xcode 9.x supports Swift 4 playground. And currently, no, you can't choose: playground is only compatible with latest stable Swift version bundled with Xcode. I.e: Xcode 9.3 will have playground in Swift 4.1. Commented Mar 14, 2018 at 17:40

3 Answers 3

20

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Surprisingly this works on the iPad Playgrounds app as well
3

By default Playground use the Swift version based on your Xcode version

You can check the Swift version by Xcode release here https://swift.org/download/#releases

2 Comments

This won’t work if you’re using the Swift Playgrounds app on your iPad. Krunal’s suggestion does work though
0

In the terminal run

swift -version 

in all probability playgrounds will be using that version

3 Comments

This doesn't really help if you have multiple versions of Xcode installed.
--find swift and look at the path. Can use xcrun to point at the relevant version (if different)
Add sudo xcode-select --switch /Applications/Xcode.app before to make sure you're running the same commandline tools as your primary Xcode.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.