- REPL:英文缩写(Read-Eval-Print-Loop)即读取-执行-输出-循环的意思。Xcode 6.1 引入了另一种以交互式的方式体验Sw ift的方法。
- 主要特点:直接运行代码,不需要创建包含调试语句的源代码文件以及进行编译、运行、查看操作。直接进行完整的读取指令、执行指令、输出结果、重新开始操作流程。
- 设置默认版本:确保Xcode是Mac计算机运行的Xcode默认版本输入命令行、回车输入电脑密码。
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer/
- 打开REPL :打开终端(Terminal)输入:xcrun swift即可唤起这个交互模式。
xcrun swift
定义常/变量1> let name:String = "ZhangSan"
name: String = "ZhangSan"2> var age:Int = 15
age: Int = 153> print("Name is:\(name),Age is:\(age)")
Name is:ZhangSan,Age is:15定义函数4> func play(name:String,age:Int) ->String{5. return "The boy Name is:\(name),Age is:\(age) playing"6. }7> play(name:name,age:age)
$R0: String = "The boy Name is:ZhangSan,Age is:15 playing"
//帮助:输入:help//退出:输入:quit//将光标移动到当前行的开始处:输入: Control+A//将光标移动到当前行的结束处:输入: Control+E
9> :helpThe 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.breakpoint -- Commands for operating on breakpoints (see 'help b'for shorthand.)command -- Commands for managing custom LLDB commands.disassemble -- Disassemble specified instructions in the currenttarget. Defaults to the current function for thecurrent thread and stack frame.expression -- Evaluate an expression on the current thread. Displays any returned value with LLDB's defaultformatting.frame -- Commands for selecting and examing the currentthread's stack frames.gdb-remote -- Connect to a process via remote GDB server. If nohost is specifed, localhost is assumed.gui -- Switch into the curses based GUI mode.help -- Show a list of all debugger commands, or givedetails about a specific command.kdp-remote -- Connect to a process via remote KDP server. If noUDP 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 currenttarget process.platform -- Commands to manage and create platforms.plugin -- Commands for managing LLDB plugins.process -- Commands for interacting with processes on thecurrent platform.quit -- Quit the LLDB debugger.register -- Commands to access registers for the current threadand stack frame.reproducer -- Commands for manipulating reproducers. Reproducersmake it possible to capture full debug sessions withall its dependencies. The resulting reproducer isused to replay the debug session while debugging thedebugger.Because reproducers need the whole the debug sessionfrom beginning to end, you need to launch thedebugger in capture or replay mode, commonly thoughthe command line driver.Reproducers are unrelated record-replay debugging,as you cannot interact with the debugger duringreplay.script -- Invoke the script interpreter with provided code anddisplay any results. Start the interactiveinterpreter if no code is supplied.session -- Commands controlling LLDB session.settings -- Commands for managing LLDB settings.source -- Commands for examining source code described bydebug information for the current target process.statistics -- Print statistics about a debugging sessiontarget -- Commands for operating on debugger targets.thread -- Commands for operating on one or more threads in thecurrent process.trace -- Commands for loading and using processor traceinformation.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 currentmodules by specifying a path to a debug symbols file or byusing the options to specify a module.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 argumentdisplays at most that many frames. The argument 'all'displays all threads. Use 'settings set frame-format' tocustomize the printing of individual frames and 'settingsset thread-format' to customize the thread header.c -- Continue execution of all threads in the current process.call -- Evaluate an expression on the current thread. Displays anyreturned 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 andstack frame.dis -- Disassemble specified instructions in the current target. Defaults to the current function for the current thread andstack frame.display -- Evaluate an expression at every stop (see 'help targetstop-hook'.)down -- Select a newer stack frame. Defaults to moving one frame, anumeric 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 thecurrent thread (see 'thread backtrace'.)file -- Create a target using the argument as the main executable.finish -- Finish executing the current stack frame and stop afterreturning. Defaults to current thread unless specified.history -- Dump the history of commands in this session.Commands in the history list can be run again using"!<INDEX>". "!-<OFFSET>" will re-run the command that is<OFFSET> commands from the end of the list (counting thecurrent command).image -- Commands for accessing information for one or more targetmodules.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 shorthandformats.list -- List relevant source code using one of several shorthandformats.n -- Source level single step, stepping over calls. Defaults tocurrent thread unless specified.next -- Source level single step, stepping over calls. Defaults tocurrent 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 anyreturned value with LLDB's default formatting.parray -- parray <COUNT> <EXPRESSION> -- lldb will evaluate EXPRESSIONto get a typed-pointer-to-an-array in memory, and willdisplay COUNT elements of that type from the array.po -- Evaluate an expression on the current thread. Displays anyreturned value with formatting controlled by the type'sauthor.poarray -- poarray <COUNT> <EXPRESSION> -- lldb will evaluateEXPRESSION to get the address of an array of COUNT objectsin memory, and will call po on them.print -- Evaluate an expression on the current thread. Displays anyreturned 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 andstack frame.repl -- Evaluate an expression on the current thread. Displays anyreturned value with LLDB's default formatting.run -- Launch the executable in the debugger.s -- Source level single step, stepping into calls. Defaults tocurrent 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 stepdirectly into a function whose name matches theTargetFunctionName.step -- Source level single step, stepping into calls. Defaults tocurrent 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 shorthandformats.undisplay -- Stop displaying expression at every stop (specified bystop-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 allarguments and local variables in scope. Names of argument,local, file static and file global variables can bespecified. Children of aggregate variables can be specifiedsuch as 'var->child.x'. The -> and [] operators in 'framevariable' do not invoke operator overloads if they exist,but directly access the specified element. If you want totrigger operator overloads use the expression command toprint the variable instead.It is worth noting that except for overloaded operators,when printing local variables 'expr local_var' and 'framevar local_var' produce the same results. However, 'framevariable' is more efficient, since it uses debug informationand memory reads directly, rather than parsing andevaluating an expression, which may even involve JITing andrunning code in the target program.var -- Show variables for the current stack frame. Defaults to allarguments and local variables in scope. Names of argument,local, file static and file global variables can bespecified. Children of aggregate variables can be specifiedsuch as 'var->child.x'. The -> and [] operators in 'framevariable' do not invoke operator overloads if they exist,but directly access the specified element. If you want totrigger operator overloads use the expression command toprint the variable instead.It is worth noting that except for overloaded operators,when printing local variables 'expr local_var' and 'framevar local_var' produce the same results. However, 'framevariable' is more efficient, since it uses debug informationand memory reads directly, rather than parsing andevaluating an expression, which may even involve JITing andrunning code in the target program.vo -- Show variables for the current stack frame. Defaults to allarguments and local variables in scope. Names of argument,local, file static and file global variables can bespecified. Children of aggregate variables can be specifiedsuch as 'var->child.x'. The -> and [] operators in 'framevariable' do not invoke operator overloads if they exist,but directly access the specified element. If you want totrigger operator overloads use the expression command toprint the variable instead.It is worth noting that except for overloaded operators,when printing local variables 'expr local_var' and 'framevar local_var' produce the same results. However, 'framevariable' is more efficient, since it uses debug informationand memory reads directly, rather than parsing andevaluating an expression, which may even involve JITing andrunning 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>'.