objective-c 如何在 Xcode 调试器中查看 NSDictionary 变量的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/112796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to view contents of NSDictionary variable in Xcode debugger?
提问by Dara Kong
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:
有没有办法通过 Xcode 调试器查看 NSDictionary 变量的键/值对?这是在变量窗口中完全展开时的信息范围:
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).
我期待它向我展示字典的每个元素(类似于数组变量)。
回答by craigb
In the gdb window you can use poto inspect the object.
在 gdb 窗口中,您可以po用来检查对象。
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 NSStringobjects that print nicely. YMMV with other complex objects.
当然,这些是NSString打印得很好的对象。YMMV 与其他复杂对象。
回答by Jens Ayton
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 -debugDescriptionmethod, which by default calls -description. Unfortunately, NSDictionaryoverrides 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.
您可以右键单击任何对象(ObjC 或 Core Foundation)变量并选择“将描述打印到控制台”?(也在运行->变量视图中)。这将打印 objct-debugDescription方法的结果,该方法默认调用-description. 不幸的是,NSDictionary覆盖它以生成一堆您通常不关心的内部数据,因此在这种特定情况下,craigb 的解决方案更好。
The displayed keys and values also use -description, so if you want useful information about your objects in collections and elsewhere, overriding -descriptionis a must. I generally implement it along these lines, to match the format of the default NSObjectimplementation:
显示的键和值也使用-description,因此如果您想要有关集合和其他地方的对象的有用信息,-description则必须进行覆盖。我通常按照这些方式实现它,以匹配默认NSObject实现的格式:
-(NSString *) description
{
return [NSString stringWithFormat:@"<%@ %p>{foo: %@}", [self class], self, [self foo]];
}
回答by uranpro
You can use CFShow()
您可以使用 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;
}
回答by jkatzer
XCode 4.6 has added the following functionality which may be helpful to you
XCode 4.6 添加了以下功能,可能对您有所帮助
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!
现在您可以检查这些对象类型,而无需在控制台中打印整个对象。享受!
来源:http: //developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_4_6.html
回答by Taiko
Click on your dict, then click on the little "i" icon, it should do the job :-)

单击您的 dict,然后单击小“i”图标,它应该可以完成工作:-)

回答by Pat
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:
如果您想在现代 XCode 中的断点操作中打印这些(是的,我在原始帖子之后 10 年!)在“日志消息”操作中使用以下断点表达式:
@myDictionary.description@
@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:
:
下面是我的断点操作的屏幕截图,其中变量 event 是 NSString,变量 contextData 是我正在记录以下内容的 NSDictionary
::
回答by user1873574
You can also use NSLog.
您也可以使用NSLog。
Also you can go in Debug area or xcode, then find out All Variables, Registers, Globals and Staticsthen select your variable. Right click on it. Then select Print description of "...."
您也可以进入调试区域或 xcode,然后找出All Variables, Registers, Globals and Statics然后选择您的变量。右键单击它。然后选择Print description of "...."
Hope it helps!
希望能帮助到你!

