Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 335
Ben Zotto Avatar asked Jul 14 '10 18:07

Ben Zotto


People also ask

How do I see variables in Xcode?

There is 2 ways to watch a variable and break at certain condition. Control-click a breakpoint indicator to display a command menu and choose Edit Breakpoint to open the breakpoint editor and set conditions, add actions, and so forth, as mentioned in Breakpoints. Use LLDB command line.

What is LLDB in Xcode?

LLDB is a debugging component used in the LLVM project which was developed by the LLVM developer group. Xcode uses the LLDB as the default debugging tool. The full form of LLDB is Low-level debugger. Breakpoints help a developer to stop the execution of the program at any point.

How do I print a variable in Xcode?

Print a variableClick the Add Action button and select Debugger Command. Put any print command in a debug console in a text field. Make sure you prefix it with po , e.g., po print(view) . Check Automatically continue after evaluating actions.

How do I debug in Xcode step by step?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.


2 Answers

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

like image 106
Gobra Avatar answered Oct 13 '22 19:10

Gobra


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.

like image 27
Kendall Helmstetter Gelner Avatar answered Oct 13 '22 18:10

Kendall Helmstetter Gelner