评估LLDB中的Swift表达

发布于 2025-01-21 12:29:01 字数 372 浏览 0 评论 0原文

我想定义一个快速扩展名,包括方法 /计算的VAR,仅用于LLDB调试目的。这是常规的Swift代码:

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

在纯LLDB中的示例中,如何定义ElementSover30?我认为表达式命令是正确使用的工具,但有时我会出现解析错误。

另外,LLDB中的其他Swift符号是否有等效的语法,例如struct s?

I want to define a Swift extension, including a method / computed var, only for LLDB debugging purposes. Here is the regular Swift code:

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? I think expression command is the right tool to use, but I sometimes get parsing errors.

Also, is there an equivalent syntax for other Swift symbols in LLDB, like structs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燕归巢 2025-01-28 12:29:01

使用LLDB中的SWIFT代码

Working with Swift code in LLDB ????

Most Swift code can be executed as part of LLDB if it's part of the stdlib with the right syntax. 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.

Extensions ????

With improvements to LLDB, you can actually copy-paste the Swift code directly after expression.

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.

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

Sometimes, we need to read Swift code using Objective-C in LLDB. For example, if we have a Swift file with a View Controller:

class LoginViewController: UIViewController {
...
}

We can print the dynamic type of the result of a memory address as follows:

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

View Swift docs ????

Use type lookup to read a minified version of the type definitions:

(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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文