简体   繁体   English

在 LLDB 中评估 Swift 个表达式

[英]Evaluate Swift expressions in LLDB

I want to define a Swift extension, including a method / computed var, only for LLDB debugging purposes.我想定义一个 Swift 扩展,包括一个方法/计算变量,仅用于 LLDB 调试目的。 Here is the regular Swift code:这是常规的 Swift 代码:

extension Collection where Self.Element == Int {
  var elementsOver30: [Self.Element]? {
    return self.filter { $0 > 30 }
  }
}

How can I define elementsOver30 in this example in pure LLDB?我如何在纯 LLDB 中定义此示例中的elementsOver30 I think expression command is the right tool to use, but I sometimes get parsing errors.我认为expression命令是正确的工具,但我有时会遇到解析错误。

Also, is there an equivalent syntax for other Swift symbols in LLDB, like struct s?此外,LLDB 中的其他 Swift 符号是否有等效语法,如struct s?

Working with Swift code in LLDB在 LLDB 中使用 Swift 代码

Most Swift code can be executed as part of LLDB if it's part of the stdlib with the right syntax.大多数 Swift 代码可以作为 LLDB 的一部分执行,如果它是具有正确语法的标准库的一部分。 The key is to prefix type names with the symbol identifier $ .关键是在类型名称前加上符号标识符$ I've used $ even for variable names here (new LLDB improvements make this unnecessary) because I prefer to distinguish LLDB definitions.我在这里使用$ even 作为变量名(新的 LLDB 改进使它变得不必要)因为我更喜欢区分 LLDB 定义。

Extensions扩展

With improvements to LLDB, you can actually copy-paste the Swift code directly after expression.通过对 LLDB 的改进,您实际上可以在表达式后直接复制粘贴 Swift 代码。

I've added an example for your extension with $ symbols for clarity:为了清楚起见,我为您的扩展添加了一个带有$符号的示例:

(lldb) expression
extension Collection where Self.Element == Int {
var $elementsOver30: [Self.Element]? {
return self.filter { $0 > 30 }
}
}
(lldb) po [32,31,4].$elementsOver30
▿ Optional<Array<Int>>
  ▿ some : 2 elements
    - 0 : 32
    - 1 : 31

Pressing Enter after expression prompts a multi-line evaluation where we can input the remaining Swift code.expression后按Enter会提示多行计算,我们可以在其中输入剩余的 Swift 代码。

Structs/Class definitions结构/类定义

(lldb) expression
struct $Person {
let name: String
}
(lldb) po let $pranav = $Person.init(name: "pranav")
(lldb) po $pranav
▿ $Person
  - name : "pranav"

Reading a Swift instance from memory从 memory 读取一个 Swift 实例

Sometimes, we need to read Swift code using Objective-C in LLDB.有时,我们需要在 LLDB 中使用 Objective-C 读取 Swift 代码。 For example, if we have a Swift file with a View Controller:例如,如果我们有一个 Swift 文件,其视图为 Controller:

class LoginViewController: UIViewController {
...
}

We can print the dynamic type of the result of a memory address as follows:我们可以打印一个 memory 地址的结果的动态类型,如下所示:

(lldb) expr -O --language objc -- 0x14160c260
<SharedSettings.LoginViewController: 0x14160c260>

View Swift docs查看 Swift 文档

Use type lookup to read a minified version of the type definitions:使用type lookup来读取类型定义的缩小版本:

(lldb) type lookup Equatable
protocol Equatable {
  static func == (lhs: Self, rhs: Self) -> Swift.Bool
}
extension Swift.Equatable {
  static func != (lhs: Self, rhs: Self) -> Swift.Bool
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM