Swift REPL

什么是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
swift1

我們來寫個簡單的例子來看看REPL的一些反饋和效果

swift2
swift2

多行的例子,這里使用了隱性定義字符串的方式。

swift3
swift3
swift4
swift4

另外我們如果定義一個方法的話,這個也是我覺得體驗很好的一點,如下

swift5
swift5

另外如果需要使用上面寫過的方法或者重新要調試的話,可以通過上下進行整個方法每一行的調用。如下

swift6
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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容