Apple has demonstrated the Swift language thus far by using the Read-Eval-Print-Loop (REPL) in Xcode playgrounds, but there is another way to work in real-time with the newest language on the block: through the command line. That’s right, you can utilize the power of the Swift REPL right from the Terminal in OS X with a little bit of work. I’ll show you how to install and use this development tool anywhere you’ve got command line access.
What you’ll need
- The latest build of Xcode 6 beta
- Xcode command line tools
Both tools are available from the downloads sections of the iOS or OS X Developer Portal.
After you install the latest Xcode 6 beta and install the command line tools, it’s time to tell the command line version of Xcode which version you now wish to use. To do this, open the Terminal application on your Mac (located in /Applications/Utilities), and type the following Terminal command, followed by the Return key (Figure A).
sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer
Using this command, you can set the default version of Xcode that is used for the command line.
The Xcode 6 binaries are named differently with each release, so you’ll need to replace the path that includes Xcode6-Beta.app in the above command to match your current version of the Xcode beta. If you haven’t authenticated recently as an administrator of your Mac, you’ll be prompted to enter your OS X administrator password, as this command requires superuser access.
If you have build scripts or environments that require a non-beta version of Xcode and interact with Xcode through the command line utilities, then switching versions of Xcode may interfere with your setup. If this is the case, you’ll want to switch back using the same command above to a non-beta version of Xcode after completing this tutorial.
Starting the REPL interface
After you run the command above and select the Xcode 6 beta application as the default version for the command line, you’ll be able to enter the Swift environment extremely easily by typing either of the following two commands (Figure B). Pick a command that’s easy to remember and use (you can set an alias for the command, if you want) — both commands will perform the same action. There is no need to run both commands, and doing so will result in two REPL environments being loaded.
After entering the command to start the REPL, the interface will change to the Swift REPL UI.
After entering one of the above commands, you’ll be placed directly into the Swift REPL. You can write any code right into the Terminal, and then press the Enter key to see it run right before your eyes.
All code that can be typed into the Xcode playgrounds can be typed and ran right here… after all, it’s the same REPL, but it doesn’t have the same pretty interface that Xcode provides with playgrounds. You can even assign variables, use loops, and more (Figure C).
Enter variables, use loops, and any other Swift code right from the command line version of the Swift REPL.
When you’re ready to exit, you can just close the Terminal window (you’ll be prompted about whether you’re sure you wish to do so). If at any time you need help, type :help onto a new line in the REPL to see the help file.