Ben Zotto Ben Zotto - 3 months ago 14
Objective-C Question

Intelligent Obj-C variable contents while debugging in Xcode?

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

isa
s 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
NSString
s, I just want to see the string values, not complex nested objects. Same goes for
NSSet
s,
NSArrays
, and the bytes inside an
NSData
. And
NSString
s, 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.

Answer

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.