REPL is abbreviation of Read Eval Print Loop which is interactive computer programming environment. This could be simply assumed that an interpreter program runs which takes the commands or instruction and immediately executes them and shows the output. All Unix/Linux shells are best example of REPL.
$ echo Hello World
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "Hello World"
Apple has provided REPL environment for Swift so that we can do experiments with Swift code and learn the language easily with less effort. To use REPL on command line open your terminal shell and type swift on it. You would be able to see something like below image. A welcome message with version of swift.
In the above image you can see the version of swift on my macbook is 2.1.1. Then you can see the command prompt with line number like 1>. On this prompt we can write single line instruction as well as multiline swift code and see how does it work. Lets do some experiments :)
We can run the swift code written in a file or multiple files.
In this section we will see how to do that.
Lets create a file named ex01.swift and write following code in and save it.
I will prefer first way to use REPL for swift code execution. I will return to multi-file source code compilation in latter tutorials.
You can compile your code using swiftc command and output would be a platform dependent CLI based application. When swift code is compiled it provides an executable file and .dSYM file. You can run executable file using command on terminal shell. Lets see how to do that.
First we have to create a swift source code, so lets name it hello.swift and write a "hello world" program
Now we are to compile this code. So follow the steps
1. Open Terminal program. In MacOSX you will find it at /Applications/Utilities
2. Go to proper folder where your swift code file is saved.
cd Desktop/swift [In my case there is a swift folder on my desktop]
3. Compile the source code
$ swiftc hello.swift
This command will compile the code and if everything is found correct in code then will produce the output executable file. Since we have not mentioned any output file name to it, so it will generate hello as output executable file.
4. Run the executable
We can mention the output file name while compiling and then we can run the output executable as follows:
You might come up in a situation where you want to read some arguments to run your programme. For example you have written a code which will copy one file into another file. So you have to read input file path and output file path. To achieve this requirement you have the enum named Process.
Process has 3 property as follows -
var inputArguments = Process.arguments
let scanner = NSScanner(string: inputArguments)
var index: Int = Int.min
Above 4 lines of code reads an integer from command line when a program is executed.
Example: ./example 100
Here example is an executable which takes 100 as input to execute.
We can run the REPL on Xcode's lldb debugger console. To do this we need some project already written or we can create a simple hello world project and put the debug point at some place. When debug point reaches to the step (statement) then we can see the console is active to take commands. Here we type a command repl and our debugger is now into swift repl mode.
To quit from REPL mode you can use any of the following commands
To run swift repl on debugger, you do not need swift project. It could work with any Objective-C project as well.