4

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)")
rustyMagnet
  • 3,479
  • 1
  • 31
  • 41

1 Answers1

7

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

rustyMagnet
  • 3,479
  • 1
  • 31
  • 41
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • In both the C and Swift lldb expression parsers, an initial $ is used as the "namespace" indicator for lldb persistent variables. That keeps them out of the way of program variables. Variables made in lldb expressions w/o an initial $ are local to the expression. – Jim Ingham Jun 17 '18 at 16:17
  • this didn't work for me. (error: unknown type name 'let',error: use of undeclared identifier 'HelloClass' - even though it was in the symbol table). I tried connecting to the app in debug mode with xCode and lldb on the command line with lldb. I also tried this article: https://stackoverflow.com/questions/29441418/lldb-swift-casting-raw-address-into-usable-type – rustyMagnet Jun 18 '18 at 04:08
  • @rustyMagnet: I tested this with a command-line app, running directly from Xcode. I am afraid that I do not have a better solution currently. – Martin R Jun 19 '18 at 14:12
  • @MartinR I have found the answer. It seems something is setting the lldb expression -l automatically after the appDelegate runs. – rustyMagnet Jun 20 '18 at 14:46
  • 1
    @rustyMagnet: So `-l swift` did help finally? Glad that you solved the problem! – (But note that Swifts naming conventions are quite different from (e.g.) Python, all identifiers use camel case – upper camel case for types, lower camel case for everything else :) – Martin R Jun 20 '18 at 15:13
  • thanks @MartinR. Yeah, I thought you might spot that. I was being inconsistent on my case so I just went back to camel case... – rustyMagnet Jun 20 '18 at 15:20