ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

swift交互式体验

2021-11-22 18:02:56  阅读:212  来源: 互联网

标签:gu help 1205.0 swift version 交互式 体验 Type


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等)

标签:gu,help,1205.0,swift,version,交互式,体验,Type
来源: https://www.cnblogs.com/gulong/p/15589595.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有