Xcode 6.1 introduces a new feature to assist in the development of Swift, the read Eval Print loop ("read-evaluate-output" loop, referred to as REPL). Developers familiar with interpreted languages will feel comfortable with this command-line environment, and experienced developers can find useful features in them. To start using it, open OS X Yosemite's Terminal application and enter Swift. If the system version is OS X Mavericks, you need to enter Xcrun Swift, and then Swift Repl is started.
12 |
Welcome to Swift version 1.1 (swift-600.0.20.0). Type :help for assistance. 1> ▌ |
All you have to do is enter the SWIFT statement at the cursor and REPL will execute your code immediately. The result of the expression is automatically formatted and displayed along with the input variables and constants. The output stream of the console is naturally embedded in the interactive session:
123456 |
1> "100" .toInt() $R0: Int? = 100 2> let name = "Katherine" name: String = "Katherine" 3> println( "Hello, \(name)" ) Hello, Katherine |
Note The result of the first line command is Repl added a name, even if the expression does not have an explicit assignment name. You can use these variable names in subsequent statements to reuse them:
12 |
4> $R0! + 200 $R1: Int = 300 |
The Swift compiler also recognizes incomplete code and prompts for complete code when needed. Even the code can be indented automatically, just like in Xcode. For example, enter a function:
12 |
5> func timesTwo() { 6. ▌ |
The prompt for the next line shows the line number and a point, not the angle brackets used to indicate the new statement. So you can see at a glance if the code needs to be complete. Here you can enter the rest of the code in the method:
123 |
5> func timesTwo() { 6. return value * 2 7. }▌ |
Here are 3 noteworthy places: First, the sixth line above is automatically indented, but repl automatically ends the indentation as we enter the closing curly braces. Second, the above function refers to a parameter, but we forget to declare it beforehand, and the function needs a return type. Finally, even if you have the last line to enter, you can still easily modify them.
Multi-line History
When the code is submitted to the compiler, it is also stored in the history of the REPL, so it is very easy to modify the error. If you end up with an unfinished function and then hit enter, you get an error message:
1 |
error: use of unresolved identifier ‘value‘ |
As with most other history implementations, you can press the UP arrow "↑" to bring up the historical input. Here REPL will bring up the entire three lines of input and place the cursor at the end. Now you can edit the code to correct the input errors.
Your input history will be saved in the session, and you can record hundreds of pieces of code. Pressing the UP arrow on a blank line will bring up an earlier history input, and pressing the down arrow will bring up a closer historical input.
Multi-line editing
Even though the repl behaves like a traditional command-line editor, it provides convenient functionality to handle multiple lines of input, such as most classes and function declarations. In the example above, you can use the arrow keys to move between the lines to quickly locate the place to be modified:
123 |
5> func timesTwo(▌){ 6. return value * 2 7. } |
You can then add parameter declarations and return types:
123 |
5> func timesTwo(value: Int) -> Int▌{ 6. return value * 2 7. } |
Pressing ENTER at the current cursor will not commit the function but will insert a new line, you need to move the cursor to the end of the function and press ENTER, the function can be used:
12 |
8> timesTwo(21) $R2: (Int) = 42 |
The end point of the automatic probe statement means that you are free to enter the code, and REPL will handle your input correctly in most cases. However, there is an unexpected situation, and when a function has a common dependency, it is necessary to commit multiple declarations at once, such as the following code:
123456 |
func foo() { bar() } func bar() { foo() } |
If you enter the above code on one line, the compiler compiles the first function at the end of the third line, causing an error:
1 |
error: use of unresolved identifier ‘bar‘ |
One solution is to enter multiple functions in the same row so that you can bypass the automatic detection end point, but there is a better solution. After entering the third line, you can press the DOWN arrow button "↓" to add a line manually, so you can enter the remaining code as usual. Two function declarations are compiled together to achieve the purpose of reciprocal recursion (mutual recursion).
Quick Guide
To help you get started with REPL, here are some of the frequently used edit and navigation key combinations:
12345678910111213 |
Arrow Keys Move cursor left/right/up/down
Control+F Move cursor right one character, same as right arrow
Control+B Move cursor left one character, same as left arrow
Control+N Move cursor to end of next line, same as down arrow
Control+P Move cursor to end of prior line, same as up arrow
Control+D Delete the character under the cursor
Option+Left Move cursor to start of prior word
Option+Right Move cursor to start of next word
Control+A Move cursor to start of current line
Control+E Move cursor to end of current line
Delete Delete the character to the left of the cursor
Esc < Move cursor to start of first line
Esc > Move cursor to end of last line
|
Cocoachina is the world's largest Apple development Chinese community, the official daily time to push a variety of exciting research and development resources and tools, the introduction of app marketing experience, the latest corporate recruitment and outsourcing information, as well as the Cocos engine, Cocos Studio Development Kit, the latest news and training information. Attention can be the first time to understand the latest product and service dynamics, in hand, the world I have!
Please search the number "Cocoachina" Follow us!
Swift REPL Introductory Introduction