Search code examples
objective-cswiftlldb

How can I access a swift convenience variable from objc in lldb?


I'm trying to set some convenience variable in a swift context and to access it from a ObjC context.

(lldb) expression -l swift -- var $answerSwift = 42
(lldb) expression -o -l swift -- $answerSwift
42
(lldb) expression -o -l objc -- $answerSwift
error: use of undeclared identifier '$answerSwift'

The other way around works perfectly fine:

(lldb) expression -l objc -- int $answerObjc = 42
(lldb) expression -o -l swift -- $answerObjc
42

How can I move a value from the swift scope (?) to the objC scope?


Solution

  • It's possible to create a "temporary context" that will be evaluated and then passed in as a variable to the expression by putting it in backticks.

    So this is going to work for breakpoints in swift code:

    (lldb) expression -l swift -- var $answerSwift = 42
    (lldb) expression -o -l objc -- `$answerSwift`
    42
    

    When I have a breakpoint in swift code I tried to set an objc var like this:

    (lldb) expression -l objc -- id $label = (id)self.label
    error: use of undeclared identifier 'self'
    

    The objc context can't access the swift variable self, so it fails.

    But when putting self.label in backticks to create the temporary swift context I can assign it to the objc variable:

    (lldb) expression -l objc -- id $label = (id)`self.label`
    (lldb) expression -l objc -O -- $label
    <UILabel: 0x7f8030c03c40; frame = (44 44; 42 21); text = 'Label'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x6000019b35c0>>