The Interactive REPL Loop - APPENDICES - Understanding Swift Programming: Swift 2 (2015)

Understanding Swift Programming: Swift 2 (2015)

PART 5: APPENDICES

C. The Interactive REPL Loop

An alternative to a Playground is a command-line tool, known as the "REPL Loop," that is part of Xcode and allows you to enter short segments of code into it. It then evaluates what you have entered and responds with a printed result.

"REPL Loop" stands for "Read-Eval-Print-Loop". The idea is that the code that implements this reads what you have entered, evaluates it, prints the result, and then continues looking for new input.

It's easy and quick to run the REPL tool to check out something about Swift, since you don't need to run Xcode, you just start the terminal tool.

Running the REPL Tool

To run the REPL loop, once Xcode is installed on your machine, first get a terminal window. Typically this is done by going to the Applications folder on your Mac, then the Utilities subfolder, and double clicking on Terminal.

This will display a window with a command-line user interface, including a prompt like:

Last login: Mon Jun 29 12:18:55 on console

craig$

If you are on Mavericks (OS X version 10.9), you should enter "xcrun swift"

If you are on Yosemite or later (OS X version 10.10+), you should enter "swift".

You will then get something like this:

craig$ xcrun swift

Welcome to Swift version 1.2. Type :help for assistance.

1>

Entering Swift Statements into the REPL Loop

Entering a Swift statement will yield an immediate response with the REPL indicating the type and value of a variable:

1> var a = 1.2

a: Double = 1.2

2> let b = "hello friend"

b: String = "hello friend"

(The blue text is how the REPL displays it—it does not mean that this is a link.)

Statements on Multiple Lines

Some statements, particularly the definitions of classes and methods, usually require multiple lines. The REPL tool will allow you to enter the entire statement and not immediately evaluate it.

In such a case the prompt that you will get in such a case is a line number followed by a period rather than a line number followed by a right angle bracket. (If you get confused and get stuck in a mode where it is continuing to accept input this way and you want out, just enter "}" characters until you get back to a ">" prompt.)

3> class Fruit {

4. var skinColor = "red"

5. }

If you then create an instance of the class, it will indicate the type and provide information about the properties and what they were initialized to:

6> var c = Fruit()

c: Fruit = {

skinColor = "red"

}

The REPL tool has quite sophisticated editing tools and maintains a history from session to session. See the Apple documentation for details.

If You Have Multiple Versions of Xcode Installed

If you have multiple versions of Xcode installed (e.g., a production version and a beta version), you may need to use xcode-select to switch your REPL tool to the version of Xcode that you want to use.

Thus, if you have Xcode versions 6 and 7 both installed, and when you run the REPL tool it says that it is running Swift 1.2 (Xcode 6), but you want to run Swift 2 in the REPL, you should enter the following in the Terminal tool:

sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer

(The sudo command will ask you to enter the admin password for the Macintosh that you are on.)

Use the sudo command only when you are in the basic terminal window, before you start up the Swift REPL.

Note that Apple's practice is to call the production version of Xcode "Xcode.app" and the beta versions "Xcode-beta.app", so both can coexist in the Applications folder.

Thus the above command will switch to the version of Xcode named Xcode-beta.app:

Welcome to Apple Swift version 2.0 (700.0.38.1 700.0.53). Type :help for assistance.

1>

To get back to the production version of Swift, use:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer