A Read-Eval-Print Loop is an interactive programming language shell that (as you can tell by the name), loops trough the following states:

  • reads input from the user
  • evaluates the expression
  • prints the result of the expression The concept of such tool is certainly not a new one. It can be implemented for most programming languages and there are many of them that are distributed with a REPL. The Swift programming language also comes with a REPL. It is built on LLDB and it has some very good use cases that you can take advantage of during your Swift programming and debugging sessions.

How to get to the REPL

There are two ways to get to the Swift REPL - by launching it from the shell prompt or trough the Xcode Debug Console.

Shell Prompt

To launch the Swift REPL, just fire up your favourite terminal app and type in the following command: xcrun swift Since Xcode 6.0 is still in beta at the time of the writing of this post, you will have to set Xcode 6.0 as the default version in order for the above command to work. sudo xcode-select -switch /Applications/Xcode6-Beta?.app/Contents/Developer1 The input will switch to the language shell and at that point you are ready to type Swift code.

Xcode Debug Console

To use the tool right in Xcode, just hit a breakpoint to enter a debugging session and enter the repl command into the Debug Console.

To switch back to the lldb prompt, enter the colon (:) symbol.

REPL vs. Playgrounds

At first glance, the REPL may look like a limited and nerdy version of the Swift Playgrounds, but this is not the case. The two tools have different uses. Playgrounds are great for prototyping and quick experimentation with the language, but they cannot be used for testing and do not have a way to access 3rd party code. The power of the REPL comes from the fact that you can use it to add code to your current session and see how it would work along your preexisting codebase.

Try out the REPL and add it to your developer toolbelt. More on the topic is coming up, so follow SwiftOverload on Twitter or subscribe to the RSS Feed.

  1. Replace the ‘?’ with the actual version number of the Xcode beta.