首页 > 解决方案 > 使用 lldb 创建 Swift 类实例变量,而不是 Objective-C

问题描述

使用我的调试器 (lldb),当它是 Objective-C 代码时,我可以轻松地创建实例类。

(lldb) e id $my_hello = [hello_from_objc new]
(lldb) po $my_hello
<hello_from_objc: 0x1c4013020>
(lldb) po [$my_hello secret_objc_method]
0x000000000000002a
(lldb) po (int) [$my_hello secret_objc_method]
42

但是当代码是纯 Swift 时,我无法弄清楚如何对 lldb 的表达式命令执行相同的操作。我很容易在 Swift 代码中创建一个实例..

let my_swift_framework = Hello_Class()
print("✔️ \(my_swift_framework.samplePublicVariable)")

标签: objective-cswiftlldb

解决方案


这是一个示例:执行 Swift 代码后

class HelloClass {
    func hello() {
        print("hello")
    }
}

您可以在调试器窗口中创建一个对象:

(lldb) expression let $myHello = HelloClass()
(lldb) po $myHello
<hello_class: 0x101121180>

(lldb) po $myHello.hello()
hello

如果你得到一个错误

error: unknown type name 'let'
error: use of undeclared identifier 'HelloClass'

然后将表达式语言显式设置为 Swift:

(lldb) expression -l swift -o -- let $myHello = HelloClass()

(lldb) expression -l swift -o -- $myHello.hello()

或者您可以将 lldb 语言上下文更改回 Swift:

(lldb) settings set target.language swift


推荐阅读