开发者

Intelligent Obj-C variable contents while debugging in Xcode?

开发者 https://www.devze.com 2023-01-07 08:43 出处:网络
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.

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, 开发者_JAVA技巧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.


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


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.


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:

Intelligent Obj-C variable contents while debugging in Xcode?

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

Intelligent Obj-C variable contents while debugging in Xcode?

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.

Intelligent Obj-C variable contents while debugging in Xcode?

Intelligent Obj-C variable contents while debugging in Xcode?

0

精彩评论

暂无评论...
验证码 换一张
取 消