使用Swift REPL
REPL是英文Read-Eval-Print Loop的缩写,一种简单的交互式运行编译环境。安装完XCode之后,就有Siwift REPL了,打开终端输入"Swift"即可启动Swift REPL。
打开Swift REPL 输入
print("Helle world")
显示如图
![](https://img.haomeiwen.com/i1904333/79e1e7022745c443.png)
使用Playground
由于Swift REPL在终端编写和运行Swift程序,操作界面很不友好,在Xcode6之后,苹果就提供了封装了Swift REPL的Playground工具。
打开Xcode->Get started with a playground->Blank->命名及选择存储位置
输入
import Foundation
var str = "Hello world"
print(str)
界面如图
![](https://img.haomeiwen.com/i1904333/d90201512d9c5f33.png)
右边区域为运行结果视图,playground会实时运行,可点击右边的长方形或小眼睛快速查看运行结果。
Playground使用便捷,可用于学习,测试算法,验证想法和可视化运行结果
网友评论