zoukankan      html  css  js  c++  java
  • Swift 交互式体验

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

  • 相关阅读:
    Pascal Analyzer 4 代码分析使用简要说明
    界面动态配置:持久化反持久化
    (Sql Server)数据的拆分和合并
    javascript中的promise和deferred:实践(二)
    对面向接口编程、按分层建项目的反思和新的分层结构思路
    revel框架教程之权限控制
    等待与通知机制
    实现代码编辑器
    自定义html标签
    javascript生成自定义的arcgis simpletoolbar
  • 原文地址:https://www.cnblogs.com/gulong/p/15589595.html
Copyright © 2011-2022 走看看