Search code examples
objective-cswiftlldb

Create Swift class instance variable with lldb, versus Objective-C


Using my debugger (lldb) I can easily create Instances classes when it is Objective-C code.

(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

But I can't work out how to do the same with lldb's expression command when the code is pure Swift. I create an instance in Swift code easily enough..

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

Solution

  • Here is an example: After executing the Swift code

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

    you can create an object in the debugger window:

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

    If you get an error

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

    then set the expression language to Swift explicitly:

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

    Or you can change the lldb language context back to Swift:

    (lldb) settings set target.language swift