使用 lldb 创建 Swift class 实例变量,而不是 Objective-C

Create Swift class instance variable with lldb, versus 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

但是当代码是纯代码时,我不知道如何用 lldb 的表达式命令做同样的事情 Swift。我很容易在 Swift 代码中创建一个实例..

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

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

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

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

(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