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.

Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54
  • This is the same answer as: http://stackoverflow.com/questions/37390238/how-can-i-set-lldbs-default-language-to-swift but swift -> objc. – Jim Ingham Aug 31 '16 at 23:53

2 Answers2

17

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/...
andreskwan
  • 1,152
  • 1
  • 13
  • 13
  • 2
    Doesn't seem to work anymore (Xcode 9.3): `(lldb) help ` _(linebreak)_ `error: '' is not a known command.` _(linebreak)_ `Try 'help' to see a current list of commands.` _(linebreak)_ `Try 'apropos ' for a list of related commands.` _(linebreak)_ `Try 'type lookup ' for information on types, methods, functions, modules, etc.` – Slipp D. Thompson May 10 '18 at 22:46
11

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]
Nikola Lajic
  • 3,985
  • 28
  • 34