Swift REPL

什么是REPL

当初最早升级Xcode6.1的时候就在Command Line Tool中增加了这个特性来辅助Swift开发,全称叫做Read Eval Print Loop。其实和我们之前提到的groovysh和python是一样的一个特性,属于交互性模式,但是swift这个REPL使用下来却是体验最好的一个。可惜当初在OSX 10.9的版本时候提示需要10.10才可以使用。所以现在我们在OS X Yosemite下就可以正常使用

实践

我们在终端中输入xcrun swift即可唤起这个交互模式

swift1

我们来写个简单的例子来看看REPL的一些反馈和效果

swift2

多行的例子,这里使用了隐性定义字符串的方式。

swift3
swift4

另外我们如果定义一个方法的话,这个也是我觉得体验很好的一点,如下

swift5

另外如果需要使用上面写过的方法或者重新要调试的话,可以通过上下进行整个方法每一行的调用。如下

swift6

更多REPL的快捷键

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

你可能感兴趣的:(Swift REPL)