110

I read What's new in Xcode 6. The article introduces some new feature about Xcode 6, and it says:

Command Line

Xcode’s debugger includes an interactive version of the Swift language, known as the REPL (Read-Eval-Print-Loop). Use Swift syntax to evaluate and interact with your running app or write new code in a script-like environment. The REPL is available from within LLDB in Xcode’s console, or from Terminal.

I want to know how to get the REPL?

Community
  • 1
  • 1
riven
  • 1,476
  • 2
  • 12
  • 15
  • 3
    Here is an article I wrote, about this. https://medium.com/swift-programming/1-learn-swift-by-running-scripts-73fdf8507f4b – Santosh Jun 21 '14 at 07:35

14 Answers14

136
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.

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
56

Alternatively, if you don't want to mess up your current dev environment, you can just run:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
Kieran Andrews
  • 5,845
  • 2
  • 33
  • 57
Sebastien Windal
  • 1,394
  • 12
  • 12
  • 31
    Or, you could even take this one step further and add this to your `~/.bash_profile`: `alias swift="/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift" ` – David Beck Jun 03 '14 at 21:33
  • 6
    To be able to use the OS X SDK you have to specify the path to it as well: `/Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -sdk /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk` – Linus Unnebäck Jun 04 '14 at 19:58
  • Thanks for the idea David. I added that to the profile, but I keep getting No such file or directory, even though it is right there, and the permissions are `-rwxr-xr-x@ 1 mark2 admin 33557440 31 May 14:43 swift`. I'm not that strong in unit. Can anyone see what is wrong? – MarkAurelius Jun 09 '14 at 06:29
  • Using `xcrun --find swift` to get the path is going to be much more future proof – Mike Weller Sep 07 '15 at 10:04
  • 1
    `xcrun --toolchain com.apple.dt.toolchain.Swift_2_3 swift` is way more better. Origin answer here: http://stackoverflow.com/a/36254848/5768974 – Puttin Sep 09 '16 at 03:28
44

Step 1: Open Terminal
Step 2: Type "swift"
Step 3: There's no step 3

Example:

GoldCoast:~ macmark$ swift
Welcome to Swift!  Type :help for assistance.
  1> println("Hello, world")
Hello, world
  2> var myVariable = 42
myVariable: Int = 42
  3> myVariable = 50
  4> let myConstant = 42
myConstant: Int = 42
  5> println(myVariable)
50
  6> let label = "The width is "
label: String = "The width is "
  7> let width = 94
width: Int = 94
  8> let widthLabel = label + String(width)
widthLabel: String = "The width is 94"
  9> :exit

GoldCoast:~ macmark$ 
MacMark
  • 6,239
  • 2
  • 36
  • 41
  • Although you can continue to use `xcrun swift`, from Swift 2.1 onwards you can simply run `swift` from the command-line. – timbo Dec 29 '15 at 06:02
  • My example is from Swift 1.0 in July 2014. It was working without xcrun since ever for me. – MacMark Dec 31 '15 at 07:19
20

In Xcode 6.1.1 with Command Line Tools installed you can execute scripts by referencing directly to /usr/bin/swift the following way:

#!/usr/bin/swift

let variable: String = "string"
print("Test \(variable)")
Klaas
  • 22,394
  • 11
  • 96
  • 107
Markus Rautopuro
  • 7,997
  • 6
  • 47
  • 60
  • Confirm this to be working with swift 2.1.1 (xcode 7.2 install) – thibaut noah Feb 11 '16 at 10:54
  • 1
    Anyone who wanna play with Swift 2.3 in Xcode 8 should not miss http://stackoverflow.com/a/36254848/1298043 – Puttin Sep 09 '16 at 03:31
  • this works great — and integrates swift nicely with the bash shell way of using scripts. just need to remember to do a chmod +x on the file and you can execute swift script from anywhere with: ./myswiftscript.swift – johnrpenner May 10 '23 at 01:25
15

In the same fashion as running Swift from the Terminal, you can also execute scripts. Just use the following shebang, and run your script. (As per Chris Lattner, creator of Swift)

#!/usr/bin/env xcrun swift -i
johankj
  • 1,765
  • 16
  • 34
  • 6
    As per XCode 6 Beta 5 the `-i` flag has been removed and will lead to an error. Just let it go and use `#!/usr/bin/env xcrun swift` instead. – Jeehut Aug 12 '14 at 17:46
  • Anyone who wanna play with Swift 2.3 in Xcode 8 should not miss http://stackoverflow.com/a/36254848/1298043 – Puttin Sep 09 '16 at 03:31
10

If any one cares a simple Swift script shebang:

#!/usr/bin/env xcrun --sdk macosx swift

If specific target version is required

#!/usr/bin/env xcrun --sdk macosx swift -target x86_64-macosx10.11

If specific toolchain is required (like you want to use Swift 2.3 but you are using Xcode 8)

#!/usr/bin/env xcrun --toolchain com.apple.dt.toolchain.Swift_2_3 --sdk macosx swift -target x86_64-macosx10.11

If you want to use Swift 2.2 in your Xcode 7.3.1, let's assume Xcode 7.3.1 is located at /Applications/Xcode7.app

sudo xcode-select -s /Applications/Xcode7.app/
xcrun --sdk macosx swift

from now on the default active developer directory changed, you can check that using:

xcode-select -p

If you want to use snapshots provided by Swift.org, you should not miss Installation here.


as first answered by me in Run swift script from Xcode iOS project as build phase

Community
  • 1
  • 1
Puttin
  • 1,596
  • 23
  • 27
8

** update as of xcode6 beta 4 **

this can also be done on xcode preferences. simply go to xcode -> preferences -> locations.

for command line tools simply select the version you want from drop down list options, refer picture below. (swift requires path to be xcode6's path).

command line tools screen

I will leave my previous answer below as well.


what Kaan said and you can also use an apple script to make simple application so you can use it to switch back and forth.

open apple script > paste this below code & export it as an application so with just one click you can switch to default path or beta path (to use swift)

set xcode6Path to "xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer"
set xcodeDefaultPath to "xcode-select -switch /Applications/Xcode.app/Contents/Developer"

display dialog "set xcode sdk path to " buttons {"xcode 6", "default"} default button 1
copy result as list to {buttonPressed}
if buttonPressed is "default" then
    try
        do shell script xcodeDefaultPath with administrator privileges
    end try
else
    try
        do shell script xcode6Path with administrator privileges
    end try
end if

then run > xcrun swift

disclaimer

  1. the script assumes you have both xcode6-beta & xcode5 installed.
  2. if you're a new developer who's trying out only xcode6beta you will not need any script or setting path manually. simply run xcrun swift as the path is already set for you.
  3. when xcode6 is finally released you will need to reset your path back to default from this simple app and never use it again.
nsuinteger
  • 1,503
  • 12
  • 21
6

After installing the official Xcode 6.1 release, there is a swift command in /usr/bin/swift.

Bear in mind that if you have a Python different from the Apple-supplied Python in the path, swift can fail with ImportError: No module named site. In that case, make sure that you do export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin before calling swift.

juandesant
  • 703
  • 8
  • 16
5

The xcrun command will use the DEVELOPER_DIR environment variable to override the currently selected Xcode installation (as set with xcode-select). You can use that to construct a single command that'll run swift on the command line and put you in the REPL. That looks like this:

/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift 

and you can alias that to just 'swift':

alias swift="/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift"

As an interesting side note, you can use the same kind of invocation to run a swift script just like you'd use bash or python by adding a -i:

#!/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift -i

println("Hello World!")

Of course, once Xcode 6 is released officially and you switch to that as your default developer tools, you can drop the DEVELOPER_DIR=.. bits and just use "xcrun swift".

Jay Lyerly
  • 457
  • 4
  • 6
4

make sure you install xcode 6.0 ,but not 6.1

If you get an error:

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

just run

xcrun --sdk iphonesimulator8.0 swift

or you can

export SDKROOT="iphonesimulator8.0" 

and then

xcrun swift

Use "xcodebuild -showsdks" to list the available SDK names.

if you install xcode 6.1,just

sudo xcode-select -s /Applications/*your-Xcode-6.1-path.app*/Contents/Developer
xcrun swift
Community
  • 1
  • 1
liu lei
  • 41
  • 4
2

For XCode6, run these commands:

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

$ xcrun swift

If you get an error:

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

try:

xcrun swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk
Vinicius Miana
  • 2,047
  • 17
  • 27
1

open Terminal,

$sudo xcode-select -switch /Applications/Xcode6-Beta6.app/Contents/Developer

Notice: The Xcode6-Beta6.app should be replaced to appropriate version you installed

Then put this line alias swift='xcrun swift' to ~/.bash_profile

And,

$source ~/.bash_profile

$swift

There you go!

Johnny Zhao
  • 2,858
  • 2
  • 29
  • 26
1

With the help of Swift REPL(Read Eval Print Loop).

Developers familiar with interpreted languages will feel comfortable in this command-line environment, and even experienced developers will find a few unique features

Launch Terminal.app and type swift and press enter. You’ll then be in the Swift REPL.

        1> print("Hello Swift REPL")
     Hello Swift REPL
        2> 10 + 20
     $R0: Int = 30
        3> var name = "Yogendra Singh"
     name: String = "Yogendra Singh"
        4> print(name)
     Yogendra Singh
        5>
Yogendra Singh
  • 2,063
  • 25
  • 20
0

In the Terminal type (I verified this works with Swift version 5.7.2):

swift repl
John
  • 232
  • 3
  • 9