美文网首页
Swift-REPL介绍

Swift-REPL介绍

作者: GitArtOS | 来源:发表于2021-03-02 11:45 被阅读0次

    1.REPL

    1.1 交互式环境-REPL:

    REPLRead-Eval-Print-Loop(读取-执行-输出-循环)的首字母缩写。它是一个命令行工具,可以快速尝试Swift代码。

    1.2 特征

    它读取指令、执行指令、输出结果,再重新开始。

    1.3 优点

    它能够直接运行代码,而不用创建包含调试语句的源代码文件,再编译、运行并查看结果。

    注:这种交互性Swift有别于CObjective-C等众多编译型语言的特点之一。

    2. 如何使用REPL

    2.1 命令行
    sudo xcode-select -s /Applications/Xcode.app/Contents/Developer/
    

    系统将提示你输入管理员密码。按要求输入即可。必须执行这个命令,它是用来确保XcodeMac计算机运行的Xcode默认版本,以防止你安装的是以前的Xcode版本。只需要执行一次这个命令。它指定的设置将被保存,除非你要切换到其他Xcode版本,否则不用再执行这个命令。

    2.2 xcrun swift

    输入下面的命令并按回车以进入Swift REPL: 如果出现要求你输入密码的对话框,输入密码即可。进入Swift REPL,你将会看到以下问候消息:

     xcrun swift
    Welcome to Apple Swift version 5.3.2 (swiftlang-1200.0.45 clang-1200.0.32.28).
    Type :help for assistance.
      1> print("i love you baby")
    i love you baby
      2>  
    
    2.3 帮助和退出:

    Swift内置了REPL命令帮助信息。在提示符下输入:help可列出REPL命令清单,这些命令开头都有一个冒号,Swift使用它来区分REPL命令和Swift语句。

     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.
    Debugger commands:
      apropos           -- List debugger commands related to a word or
                           subject.
      breakpoint        -- Commands for operating on breakpoints (see 'help
                           b' for shorthand.)
      command           -- Commands for managing custom LLDB commands.
      disassemble       -- Disassemble specified instructions in the current
                           target.  Defaults to the current function for the
                           current thread and stack frame.
      expression        -- Evaluate an expression on the current thread. 
                           Displays any returned value with LLDB's default
                           formatting.
      frame             -- Commands for selecting and examing the current
                           thread's stack frames.
      gdb-remote        -- Connect to a process via remote GDB server.  If
                           no host is specifed, localhost is assumed.
      gui               -- Switch into the curses based GUI mode.
      help              -- Show a list of all debugger commands, or give
                           details about a specific command.
      kdp-remote        -- Connect to a process via remote KDP server.  If
                           no UDP port is specified, port 41139 is assumed.
      language          -- Commands specific to a source language.
      log               -- Commands controlling LLDB internal logging.
      memory            -- Commands for operating on memory in the current
                           target process.
      platform          -- Commands to manage and create platforms.
      plugin            -- Commands for managing LLDB plugins.
      process           -- Commands for interacting with processes on the
                           current platform.
      quit              -- Quit the LLDB debugger.
      register          -- Commands to access registers for the current
                           thread and stack frame.
      reproducer        -- Commands for manipulating reproducers.
                           Reproducers make it possible to capture full
                           debug sessions with all its dependencies. The
                           resulting reproducer is used to replay the debug
                           session while debugging the debugger.
                           Because reproducers need the whole the debug
                           session from beginning to end, you need to launch
                           the debugger in capture or replay mode, commonly
                           though the command line driver.
                           Reproducers are unrelated record-replay
                           debugging, as you cannot interact with the
                           debugger during replay.
      script            -- Invoke the script interpreter with provided code
                           and display any results.  Start the interactive
                           interpreter if no code is supplied.
      settings          -- Commands for managing LLDB settings.
      source            -- Commands for examining source code described by
                           debug information for the current target process.
      statistics        -- Print statistics about a debugging session
      target            -- Commands for operating on debugger targets.
      thread            -- Commands for operating on one or more threads in
                           the current process.
      type              -- Commands for operating on the type system.
      version           -- Show the LLDB debugger version.
      watchpoint        -- Commands for operating on watchpoints.
    Current command abbreviations (type ':help command alias' for more info):
      add-dsym  -- Add a debug symbol file to one of the target's current
                   modules by specifying a path to a debug symbols file, or
                   using the options to specify a module to download symbols
                   for.
      attach    -- Attach to process by ID or name.
      b         -- Set a breakpoint using one of several shorthand formats.
      bt        -- Show the current thread's call stack.  Any numeric
                   argument displays at most that many frames.  The argument
                   'all' displays all threads.
      c         -- Continue execution of all threads in the current process.
      call      -- Evaluate an expression on the current thread.  Displays
                   any returned value with LLDB's default formatting.
      continue  -- Continue execution of all threads in the current process.
      detach    -- Detach from the current target process.
      di        -- Disassemble specified instructions in the current target.
                   Defaults to the current function for the current thread
                   and stack frame.
      dis       -- Disassemble specified instructions in the current target.
                   Defaults to the current function for the current thread
                   and stack frame.
      display   -- Evaluate an expression at every stop (see 'help target
                   stop-hook'.)
      down      -- Select a newer stack frame.  Defaults to moving one
                   frame, a numeric argument can specify an arbitrary number.
      env       -- Shorthand for viewing and setting environment variables.
      exit      -- Quit the LLDB debugger.
      f         -- Select the current stack frame by index from within the
                   current thread (see 'thread backtrace'.)
      file      -- Create a target using the argument as the main executable.
      finish    -- Finish executing the current stack frame and stop after
                   returning.  Defaults to current thread unless specified.
      image     -- Commands for accessing information for one or more target
                   modules.
      j         -- Set the program counter to a new address.
      jump      -- Set the program counter to a new address.
      kill      -- Terminate the current target process.
      l         -- List relevant source code using one of several shorthand
                   formats.
      list      -- List relevant source code using one of several shorthand
                   formats.
      n         -- Source level single step, stepping over calls.  Defaults
                   to current thread unless specified.
      next      -- Source level single step, stepping over calls.  Defaults
                   to current thread unless specified.
      nexti     -- Instruction level single step, stepping over calls. 
                   Defaults to current thread unless specified.
      ni        -- Instruction level single step, stepping over calls. 
                   Defaults to current thread unless specified.
      p         -- Evaluate an expression on the current thread.  Displays
                   any returned value with LLDB's default formatting.
      parray    -- parray <COUNT> <EXPRESSION> -- lldb will evaluate
                   EXPRESSION to get a typed-pointer-to-an-array in memory,
                   and will display COUNT elements of that type from the
                   array.
      po        -- Evaluate an expression on the current thread.  Displays
                   any returned value with formatting controlled by the
                   type's author.
      poarray   -- poarray <COUNT> <EXPRESSION> -- lldb will evaluate
                   EXPRESSION to get the address of an array of COUNT
                   objects in memory, and will call po on them.
      print     -- Evaluate an expression on the current thread.  Displays
                   any returned value with LLDB's default formatting.
      q         -- Quit the LLDB debugger.
      r         -- Launch the executable in the debugger.
      rbreak    -- Sets a breakpoint or set of breakpoints in the executable.
      re        -- Commands to access registers for the current thread and
                   stack frame.
      repl      -- Evaluate an expression on the current thread.  Displays
                   any returned value with LLDB's default formatting.
      run       -- Launch the executable in the debugger.
      s         -- Source level single step, stepping into calls.  Defaults
                   to current thread unless specified.
      shell     -- Run a shell command on the host.
      si        -- Instruction level single step, stepping into calls. 
                   Defaults to current thread unless specified.
      sif       -- Step through the current block, stopping if you step
                   directly into a function whose name matches the
                   TargetFunctionName.
      step      -- Source level single step, stepping into calls.  Defaults
                   to current thread unless specified.
      stepi     -- Instruction level single step, stepping into calls. 
                   Defaults to current thread unless specified.
      t         -- Change the currently selected thread.
      tbreak    -- Set a one-shot breakpoint using one of several shorthand
                   formats.
      undisplay -- Stop displaying expression at every stop (specified by
                   stop-hook index.)
      up        -- Select an older stack frame.  Defaults to moving one
                   frame, a numeric argument can specify an arbitrary number.
      v         -- Show variables for the current stack frame. Defaults to
                   all arguments and local variables in scope. Names of
                   argument, local, file static and file global variables
                   can be specified. Children of aggregate variables can be
                   specified such as 'var->child.x'.  The -> and []
                   operators in 'frame variable' do not invoke operator
                   overloads if they exist, but directly access the
                   specified element.  If you want to trigger operator
                   overloads use the expression command to print the
                   variable instead.
                   It is worth noting that except for overloaded operators,
                   when printing local variables 'expr local_var' and 'frame
                   var local_var' produce the same results.  However, 'frame
                   variable' is more efficient, since it uses debug
                   information and memory reads directly, rather than
                   parsing and evaluating an expression, which may even
                   involve JITing and running code in the target program.
      var       -- Show variables for the current stack frame. Defaults to
                   all arguments and local variables in scope. Names of
                   argument, local, file static and file global variables
                   can be specified. Children of aggregate variables can be
                   specified such as 'var->child.x'.  The -> and []
                   operators in 'frame variable' do not invoke operator
                   overloads if they exist, but directly access the
                   specified element.  If you want to trigger operator
                   overloads use the expression command to print the
                   variable instead.
                   It is worth noting that except for overloaded operators,
                   when printing local variables 'expr local_var' and 'frame
                   var local_var' produce the same results.  However, 'frame
                   variable' is more efficient, since it uses debug
                   information and memory reads directly, rather than
                   parsing and evaluating an expression, which may even
                   involve JITing and running code in the target program.
      vo        -- Show variables for the current stack frame. Defaults to
                   all arguments and local variables in scope. Names of
                   argument, local, file static and file global variables
                   can be specified. Children of aggregate variables can be
                   specified such as 'var->child.x'.  The -> and []
                   operators in 'frame variable' do not invoke operator
                   overloads if they exist, but directly access the
                   specified element.  If you want to trigger operator
                   overloads use the expression command to print the
                   variable instead.
                   It is worth noting that except for overloaded operators,
                   when printing local variables 'expr local_var' and 'frame
                   var local_var' produce the same results.  However, 'frame
                   variable' is more efficient, since it uses debug
                   information and memory reads directly, rather than
                   parsing and evaluating an expression, which may even
                   involve JITing and running code in the target program.
      x         -- Read from the memory of the current target process.
    For more information on any command, type ':help <command-name>'.
    

    退出Swift,可以执行命令:quit

      1> :quit
    ➜  ~ 
    
    

    相关文章

      网友评论

          本文标题:Swift-REPL介绍

          本文链接:https://www.haomeiwen.com/subject/wumtqltx.html