Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view contents of NSDictionary variable in Xcode debugger?

Is there a way to view the key/value pairs of a NSDictionary variable through the Xcode debugger? Here's the extent of information when it is fully expanded in the variable window:

Variable  Value      Summary jsonDict  0x45c540   4 key/value pairs  NSObject {...}   isa     0xa06e0720 

I was expecting it to show me each element of the dictionary (similar to an array variable).

like image 632
Dara Kong Avatar asked Sep 22 '08 01:09

Dara Kong


People also ask

How do I display a variable in Xcode?

Select a variable and click the Quick Look button to see a preview of the variable, click the Print Description button to print a description of the object in the console.

How do I show debugger in Xcode?

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.

What is __ Nsdictionarym?

1. NSDictionary is a so called class cluster. It uses concrete subclasses (like __NSDictionaryI for immutable implementations) to abstract away implementation details.


2 Answers

In the gdb window you can use po to inspect the object.

given:

NSMutableDictionary* dict = [[NSMutableDictionary alloc] init]; [dict setObject:@"foo" forKey:@"bar"]; [dict setObject:@"fiz" forKey:@"buz"]; 

setting a breakpoint after the objects are added you can inspect what is in the dictionary

(gdb) po dict {   bar = foo;   buz = fiz; } 

Of course these are NSString objects that print nicely. YMMV with other complex objects.

like image 183
craigb Avatar answered Sep 23 '22 02:09

craigb


You can right-click any object (ObjC or Core Foundation) variable and select “Print Description to Console” (also in Run->Variables View). This prints the result the obejct’s -debugDescription method, which by default calls -description. Unfortunately, NSDictionary overrides this to produce a bunch of internal data the you generally don’t care about, so in this specific case craigb’s solution is better.

The displayed keys and values also use -description, so if you want useful information about your objects in collections and elsewhere, overriding -description is a must. I generally implement it along these lines, to match the format of the default NSObject implementation:

-(NSString *) description {     return [NSString stringWithFormat:@"<%@ %p>{foo: %@}", [self class], self, [self foo]]; }
like image 25
Jens Ayton Avatar answered Sep 21 '22 02:09

Jens Ayton