4

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?

3
  • Given that Swift globals aren't visible in ObjC even in normal code, I would not be surprised if this was totally impossible. (Adding @objc to the Swift expression doesn't even parse.) Commented Mar 8, 2019 at 18:58
  • Swift Int's are structs not classes. I don't think you can access swift structs in ObjC in compiled code, so you won't be able to in the debugger either. Objects could be made to work though I wouldn't be surprised if they don't work out of the box. Commented Mar 11, 2019 at 18:39
  • I used Ints because I hoped they would make a good, simple example. In my initial setup I was trying to access objects. Commented Mar 13, 2019 at 11:24

1 Answer 1

2

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>>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.