REPL:Swift交互式解释器
2020-09-06 本文已影响0人
黑白森林无间道
Xcode6.1引入的以交互式的方式来体验swift的方法
Read Eval PrintLoop:简称REPL
REPL快捷键
- 退出 :quit :q
- 帮助 :help
- 将光标移动到当前行的开始处 Control+A
- 将光标移动到当前行的开始处 Control+E
示例代码
% swift
Welcome to Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9).
Type :help for assistance.
1> let a = "100"
a: String = "100"
2> Int(a)
$R0: Int? = 100
3> let b = $R0 + 50
error: repl.swift:3:9: error: value of optional type 'Int?' must be unwrapped to a value of type 'Int'
let b = $R0 + 50
^
repl.swift:3:9: note: coalesce using '??' to provide a default when the optional value contains 'nil'
let b = $R0 + 50
^
( ?? <#default value#>)
repl.swift:3:9: note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
let b = $R0 + 50
^
!
3> let b = $R0! + 50
b: Int = 150
4> func addTwoNumbers(num1:Int, num2:Int) -> Int {
5. return num1 + num2
6. }
7> let sum = addTwoNumbers(num1: 100, num2: 50)
sum: Int = 150