swift 交互式
1. 简介
REPL: Read Eval Print Loop.
可以在终端直接敲入swift回车就能进入,在这里你做很多事
- 快速验证一些结果
- 做一些简洁的算法
- 可直接执行一个swift 文件
➜ ~ swift
Welcome to Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57).
Type :help for assistance.
1>
hello.swift 源码
func sayHi(_ str: String){
print("Hi " + str)
}
sayHi("huanggulong")
执行hello.swift
➜ test git:(master) ✗ swift < hello.swift
Welcome to Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57).
Type :help for assistance.
Hi huanggulong
➜ test git:(master) ✗
2. 帮助
在交互模式下敲入 ‘:help’ 即可
➜ ~ swift
Welcome to Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57).
Type :help for assistance.
1> :help
The REPL (Read-Eval-Print-Loop) acts like an interpreter. Valid statements, expressions, and
declarations are immediately compiled and executed.
The complete set of LLDB debugging commands are also available as described below.
Commands must be prefixed with a colon at the REPL prompt (:quit for example.) Typing just a
colon followed by return will switch to the LLDB prompt.
Type “< path” to read in code from a text file “path”.
Debugger commands:
apropos -- List debugger commands related to a word or subject.
.
.
.
For more information on any command, type ':help <command-name>'.
3. 其他命令
3.1 :quit :q :exit
退出当前进程
3.2 :version
查看swift的版本号
4. 用法
4.1 变量,对于没有会生成一个临时变量(比如 $R0)
➜ ~ swift
Welcome to Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57).
Type :help for assistance.
1> let gu = 2
gu: Int = 2
2> let long = 4
long: Int = 4
3> print(gu + long);
6
4> if gu > 1 {
5. print("变量gugu大于1")
6. }
变量gugu大于1
7> gu + long
$R0: Int = 6
8> print($R0)
6
9>
4.2 函数
➜ ~ swift
Welcome to Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57).
Type :help for assistance.
1> func add(_ m: Int, n: Int){
2. print("\(m+n)")
3. }
4> add(m:4, n:3);
error: repl.swift:4:4: error: extraneous argument label 'm:' in call
add(m:4, n:3);
^~~
4> add(4, n: 3);
7
5>
注: 可以对比ipyhon 和其他交互式命令(mysql sqlite等)