How to print string value from breakpoint action in Xcode 4?

16,563

Solution 1

There are a couple of things you can do. Add a Log function from the drop-down, then add another box and select Debugger Command and enter po f.name.

If you want your log to be more complicated, you could do something more like this:

the person name is: @(const char *)[(NSString*)[p.name description] UTF8String]@

You best bet is probably to just use the debugger's graphical interface to watch variables. If you want to log messages, use NSLog.

Solution 2

You can use NSLog statements with breakpoints using "Debugger Command", but you need to add "expr"

expr (void)NSLog(@"The Person Name is %@", p.name)

-

using expr command with a Debugger Command as breakpoint in Xcode 4.4

Solution 3

Here is a solution using only one action, and using fewer characters than the expr solution.

debugger command using NSlog

However, unless you add the void like this:

po (void)NSLog(@"the person name is: %@", p.name)

you will get an annoying "nil" printed out with your log. for example:

(lldb) po NSLog(@"foo")
 nil
2013-06-19 14:42:59.025 TheMove[95864:c07] foo

(lldb) po (void)NSLog(@"foo")
2013-06-19 14:43:10.758 TheMove[95864:c07] foo

If you can live with the nil (I can) it's faster to type and easier to remember just the po

Solution 4

I ended up using 2 different actions for the same breakpoint. First a Log and then a debugger command with po varName. Only downside it will print in 2 different rows.

enter image description here

Share:
16,563
4thSpace
Author by

4thSpace

Updated on June 10, 2022

Comments

  • 4thSpace
    4thSpace almost 2 years

    I have a breakpoint action and am using the Log option from the drop down. I'd like to print out the string (summary) value. I'm doing this:

    the person name is: @p.name@
    

    but that prints the memory address. I can switch to the Debugger Command option and do

    po f.name
    

    but then I lose my description, as used in the first option. Using the Log option, is there a way to print the string value and not the memory address?

  • futureelite7
    futureelite7 almost 12 years
    In my case, XCode simply ignores whatever I type. Does the type of debugger matter (ie GDB / LLDB)?
  • Jason Coco
    Jason Coco almost 12 years
    @futureelite7 The both GDB and LLDB should be able to do this. You could try using the "Expression" action instead of "Log Message" then just NSLog there. So, in the "Expression" window you'd type something like: (void)NSLog(@"Log this variable: %@", aVariable);
  • Philip007
    Philip007 over 11 years
    You can use one action like Jason suggests
  • Snowcrash
    Snowcrash about 11 years
    @JasonCoco it seems the Expression action is not there in Xcode 4.6.
  • Mike
    Mike about 11 years
    This had me confused for a bit, in your expression you're using “ instead of " for quotes. They look nearly identical in Xcode. For anyone else, while it was in there it was giving this error error: unexpected '@' in program.
  • auco
    auco about 11 years
    Thanks Mike; I did not notice this. It must have been automatically replaced while I copy/pasted the expression. Fixed it.
  • Oleksiy Ivanov
    Oleksiy Ivanov almost 10 years
    Sometimes such tweak helps: the person name is: @(const char *)[(NSString*)[[(id)p name] description] UTF8String]@
  • LunaCodeGirl
    LunaCodeGirl over 9 years
    I've tried to use the @expr@ syntax many times with very little success. This is a really simple workaround, thanks!
  • vond
    vond over 5 years
    That does not work currently with strings. I really wish that it did :(
  • Chad Chabot
    Chad Chabot over 5 years
    If you're doing this with multiple breakpoints that are similar, I suggest adding a prefix to your message to make it easy to filter the output in the console log. Set the action to Log Message. Set the content of the message to #### event name: @eventName@. And use *#### as the search pattern in the console log pane.
  • Jonas W
    Jonas W almost 4 years
    FYI: When using Swift, this would look like po NSLog("the person name is: %@", p.name)