8

In its default (ie, my) configuration, Xcode is somewhat unhelpful in its debugger window for variables, especially those of the common Objective-C collections variety.

The debugger seems to usually want to display the underlying Obj-C structure when I expand some object, so I'm looking at isas and the class hierarchy.

But what I almost always want here is something semantically meaningful for the object itself. E.g. for an NSDictionary, I'd ideally want to see a list of keys/values. If those keys and values are, for example NSStrings, I just want to see the string values, not complex nested objects. Same goes for NSSets, NSArrays, and the bytes inside an NSData. And NSStrings, while usually getting their string representation in the Summary column, are impossible to look at when they're long (e.g. a pathname that's too long to fit in the column doesn't seem to scroll)-- when I double-click it, I get the display template string instead, so I can't select/copy it either.

I have recently spent time in Eclipse debugging Java, and for all its faults, Eclipse knows about all the Java collections, and has a simple one-line dump out of the contents of a string or collection or whatever when you find it in the debugger.

Is there a way to get this in Xcode? Am I missing something obvious, or should I be diving into the display templating system? I know there's some support there, as NSArrays seem to get a special kind of listy format, NSDictionaries get a "2 key/value pairs" summary, etc.

EDIT: It's possible to drop into GDB to get more data on objects. I'm disheartened that GDB's po acting on an NSDictionary gives the sort of awesomely useful output that I expect from a GUI debugger. Can this be replicated without context switching to the console?

I enjoy the Xcode environment so much, but the near-complete opaqueness of objects that I use all the time really stymies debugging time. Thanks.

1
  • If I understand you correctly, I think this would be a great feature (if it exists I want to know, too) - but this shouldn't hinder debugging, as the gdb console and all its CLI glory is available to dump whatever you need. :) Commented Jul 14, 2010 at 18:29

3 Answers 3

4

Yep, XCode variables lookup during the debug is weak, but it is based on gdb and you can control it thought the console. During debug open the console and write whatever command you need, to see the NSDictionary* dic; contents it's as simple as

po dic

po prints data as presented in the [obj description] result. You can also call any methods like

po [dict valueForKey:@"myKey"], or p(NSRect) [[self view] frame]

You can get more in gdb help

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

2 Comments

+1 Thanks Gobra. I'm going to edit the question to emphasize that I'm hoping for non-commandline solutions, but I understand that this might be the best solution out there, unfortunately.
Right-clicking a variable in the full debugger window’s Variables pane shows a “Print Description to Console” command, which is the equivalent of po. (This actually calls -debugDescription, which by default calls -description but is overridden to provide more detailed information for, e.g., collections.)
4

I would look at both special GDB output (as Gobra noted), but also the display templates.

The display stuff looks complex but is actually pretty simple - here's an example for makign NSIndexPath display "Sec:x Row:y":

Sec:{(int)[$VAR section]}  Row:{(int)[$VAR row]} 

So you can print descriptive text and multiple values for any object type. The display variables work for all classes of that type, and persist between XCode runs and also across projects.

One last tricky thing to note is that for anything that returns a string, you need to add a ":s" after the "{}" pair like so:

{[myClass description]}:s

If you need to clear a display template, just click the line to edit and erase it all - you go back to the default. So, it's really easy to quickly create temporary formatters for any object that let you see exactly what is of interest.

Comments

1

In Xcode 6 this feature seems to have been implemented? I cannot speak to the features of Xcode when this question was asked in 2010.

NSString, NSNumber, NSArray, and NSDictionary values in your code, with a breakpoint:

variables in code

When the breakpoint breaks, you can see the values in the Xcode "Variables View". For collections you may need to expand the details arrow:

values in the debugger

If you right-click a variable and select "Print Description of variable_name", the object's -debugDescription / -description value will be printed to the console. This is often useful for more complex collections, and perhaps for long NSString values and etc.

Screen shot in Xcode 7 of “Print Description” context-menu item

printed to the console.

2 Comments

Yes, and thank you for the updated answer. Xcode has slowly rolled in these improvements in default display over the last few years-- in 2010 it was shocking how not-useful the defaults were.
Is there a keyboard shortcut for the Print Description context-menu item?

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.