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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Xcode 9 - Swift 4. Xcode 8 - Swift 3. – rmaddy Mar 14 '18 at 05: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. – Krunal Mar 14 '18 at 05: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. – rmaddy Mar 14 '18 at 05: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? – Krunal Mar 14 '18 at 05: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. – Cœur Mar 14 '18 at 17:40

3 Answers3

19

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.

Hemang
  • 26,840
  • 19
  • 119
  • 186
Krunal
  • 77,632
  • 48
  • 245
  • 261
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

ferbass
  • 859
  • 5
  • 17
  • 1
    A small warning: [Some Apple documentation is incorrect regarding which Swift version is available for Xcode](https://stackoverflow.com/questions/49102875/xcode-9-2-is-not-showing-swift-4-1). – Cœur Mar 15 '18 at 07:12
  • This won’t work if you’re using the Swift Playgrounds app on your iPad. Krunal’s suggestion does work though – mfaani May 05 '21 at 23:04
0

In the terminal run

swift -version

in all probability playgrounds will be using that version

stevenpcurtis
  • 1,907
  • 3
  • 21
  • 47
  • 2
    This doesn't really help if you have multiple versions of Xcode installed. – rmaddy Mar 14 '18 at 05:52
  • --find swift and look at the path. Can use xcrun to point at the relevant version (if different) – stevenpcurtis Mar 14 '18 at 06:43
  • 1
    Add `sudo xcode-select --switch /Applications/Xcode.app` before to make sure you're running the same commandline tools as your primary Xcode. – Cœur Mar 15 '18 at 07:11