Python 在 Swift REPL 中的 dir() 和 help() 等价物

Python's dir() and help() equivalents in Swift REPL

刚刚在 Ubuntu 上安装了 Swift 并且惊喜地发现执行 swift 会运行一种语言 shell(又名 REPL),类似于 Python'互动 shell.

为了感受 Swift 语言,这对我来说是新的,我想声明一些对象并查看它们提供的方法。在 Python 中,这是通过内置函数 dir(object) 实现的,其中 returns 对象属性的名称。如果您不带参数调用它,它会 returns 当前局部范围内的变量和模块名称列表。 – 这两种行为在 shell.

中都非常有用

Python shell 还有一个非常有用的 help() 功能,可以调用内置的帮助系统。通过调用help(object),可以读取对象class上的Python引用。

Swift REPL 中是否有 Python 的 help() 和 dir() 的等价物?

更具体地说:

(如果有帮助,我有Swift版本4.2.1,LLDB版本6.0.0)

Swift 没有help()』这样的方法,但是你可以使用 lldb 命令。 您可以在 Swift REPL 中输入 : 以便 shell 切换到 LLDB 模式。

Can I see the list of object methods in the shell (REPL)?
type lookup ClassNameOfInstance

Can I get the list of currently declared variables?
image lookup -rs lldb_expr

Can I read object reference in the shell?
expression -l swift -- instanceName

使用c命令退出LLDB模式。

加法:

  • 当你在Swift REPL 中定义变量时,Swift Repl 会为每个变量创建$__lldb_expr2$__lldb_expr3 等模块。

  • 如果您想了解 lldb 命令,可以在 lldb 模式下使用 help 命令。