13

When I am debugging a Swift app in Xcode, the debugger expects expressions in Swift format. How can I switch it to expect Objective-C expressions instead?

E.g., I want to be able to type expr id $foo = [[SomeClass alloc] initWithBar:@"quux"]; instead of whatever the Swift equivalent is.

1

2 Answers 2

18

Swift 3.0 or before use: You can use the following command to identify the name of all available languages in LLDB.

(lldb)help <language>

Swift 4.0

(lldb)help <source-language>

Create an alias like "eco" to print objective-c objects:

(lldb)command alias eco expression -l objective-c -o --
(lldb)eco [[UIApplication sharedApplication] userHomeDirectory] 
/Users/...
1
  • 2
    Doesn't seem to work anymore (Xcode 9.3): (lldb) help <language> (linebreak) error: '<language>' is not a known command. (linebreak) Try 'help' to see a current list of commands. (linebreak) Try 'apropos <language>' for a list of related commands. (linebreak) Try 'type lookup <language>' for information on types, methods, functions, modules, etc. May 10, 2018 at 22:46
12

Advanced debugging with Xcode and LLDB from WWDC 2018, shows that this can be done with the following command:

expression -l objc -O -- [doYourStuff here]

but there is an important thing to note here, switching from a Swift to an Objective C frame creates a new context. In practice this means that you will probably get an error like:

error: use of undeclared identifier 'self'

to avoid this problem you need to enclose any varables, that need to be evaluated before the context change, in backticks `. For example:

expression -l objc -O -- [`self` class]
1
  • Xcode 12 gives me: use of undeclared identifier 'l' Feb 26, 2021 at 11:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.