什么是REPL
當初最早升級Xcode6.1的時候就在Command Line Tool中增加了這個特性來輔助Swift開發(fā),全稱叫做Read Eval Print Loop。其實和我們之前提到的groovysh和python是一樣的一個特性,屬于交互性模式,但是swift這個REPL使用下來卻是體驗最好的一個??上М敵踉贠SX 10.9的版本時候提示需要10.10才可以使用。所以現(xiàn)在我們在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