How to view contents of NSDictionary variable in Xcode debugger?

Objective CCocoaXcodeDebugging

Objective C Problem Overview


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

Objective C Solutions


Solution 1 - Objective C

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.

Solution 2 - Objective C

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]];
}

Solution 3 - Objective C

You can use CFShow()

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

In output you will see

{
  bar = foo;
  buz = fiz;
}

Solution 4 - Objective C

XCode 4.6 has added the following functionality which may be helpful to you

The elements of NSArray and NSDictionary objects can now be inspected in the Xcode debugger

Now you can inspect these object types without having to print the entire object in the console. Enjoy!

Source: http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_6.html

Solution 5 - Objective C

Click on your dict, then click on the little "i" icon, it should do the job :-) Xcode5, view the value of a dict

Solution 6 - Objective C

If you would like to print these in a breakpoint action in modern XCode (yes, I am 10 years after the original post!) use the following breakpoint expression in a "Log Message" action:

@myDictionary.description@

Below is a screenshot of my breakpoint action where the variable event is an NSString and the variable contextData is the NSDictionary that I am logging the contents of: :

Solution 7 - Objective C

You can also use NSLog.

Also you can go in Debug area or xcode, then find out All Variables, Registers, Globals and Statics then select your variable. Right click on it. Then select Print description of "...."

Hope it helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDara KongView Question on Stackoverflow
Solution 1 - Objective CcraigbView Answer on Stackoverflow
Solution 2 - Objective CJens AytonView Answer on Stackoverflow
Solution 3 - Objective CuranproView Answer on Stackoverflow
Solution 4 - Objective CjkatzerView Answer on Stackoverflow
Solution 5 - Objective CTaikoView Answer on Stackoverflow
Solution 6 - Objective CPatView Answer on Stackoverflow
Solution 7 - Objective Cuser1873574View Answer on Stackoverflow