swift 交互式
1. 简介
REPL: Read Eval Print Loop.
可以在终端直接敲入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 @R_119_4045@ion on any command, type ':help <command-name>'.
3. 其他命令
3.1 :quit :q :exit
退出当前进程
3.2 :version
查看swift的版本号
4. 用法
➜ ~ 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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。