如何转储存储在 Objective-c 对象(NSArray 或 NSDictionary)中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/289241/
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 dump data stored in objective-c object (NSArray or NSDictionary)
提问by jpm
Forgive me for a potentially silly question here, but in other programming languages (scripting ones like PHP or Perl) it is often easy to dump everything contained within a variable.
请原谅我在这里提出一个潜在的愚蠢问题,但在其他编程语言(PHP 或 Perl 等脚本语言)中,通常很容易转储变量中包含的所有内容。
For instance, in PHP there are the var_dump()or print_r()functions. Perl has the Data::DumperCPAN class, etc etc.
例如,在 PHP 中有var_dump()orprint_r()函数。Perl 有Data::DumperCPAN 类等等。
Is there something like this for Objective-C? It would be very convenient in a few cases to be able to dump everything like that, instead of using gdb to inspect each variable.
Objective-C 有类似的东西吗?在某些情况下,能够像这样转储所有内容,而不是使用 gdb 来检查每个变量会非常方便。
回答by Barry Wark
In Cocoa, there is no "dump" like PHP's print_r or python's reprsince there is no textual format that "represents" an object as in those languages. If you use
在 Cocoa 中,没有像 PHP 的 print_r 或 python 的repr那样的“转储”,因为没有像在这些语言中那样“表示”对象的文本格式。如果你使用
NSLog(@"%@", myObj);
or
或者
NSString *stringRep = [NSString stringWithFormat:@"%@",myObj];
or
或者
NSString *stringRep = [myObj description];
you will get (logged to console in the first case), the result of [myObj description], a method defined in NSObjectfor the purpose of printing a description(nota dump) of an object.
您将获得(在第一种情况下登录到控制台)的结果[myObj description],在 中定义的方法NSObject用于打印对象的描述(而不是转储)。
If you invoke po myObjin gdb, you get [myObj debugDescription](often the same as description, but not always).
如果您po myObj在 gdb 中调用,您会得到[myObj debugDescription](通常与 相同description,但并非总是如此)。
Classes like NSArrayand NSDictionaryand NSDataoverride descriptionto print a pretty useful recursive description of their contents, but the default [NSObject description]prints only the pointer value corresponding to the instance.
像NSArrayandNSDictionary和NSDataoverride 之类的类description可以打印非常有用的对其内容的递归描述,但默认值[NSObject description]仅打印与实例对应的指针值。
If you control the code for the types in question, you can override their descriptionor debugDescriptionmethods to return anything you want. If not, you could override the descriptionor debugDescriptionmethod using a category, or use a category to define a myDebugDescriptionor some such that you could then invoke from gdb using po [myObj myDebugDescription].
如果您控制相关类型的代码,您可以覆盖它们的description或debugDescription方法以返回您想要的任何内容。如果没有,您可以使用类别覆盖descriptionordebugDescription方法,或者使用类别定义 amyDebugDescription或 some 以便您可以使用po [myObj myDebugDescription].
回答by Ben Gottlieb
you can also use the gdb print object command to quickly view an object in the debugger:
您还可以使用 gdb 打印对象命令在调试器中快速查看对象:
po dictionary
This will be basically the same as calling NSLog(...) from within your code.
这与在代码中调用 NSLog(...) 基本相同。
Also useful when printing out NSData that contains ASCII data is:
在打印出包含 ASCII 数据的 NSData 时也很有用:
p (char *) [data bytes]
回答by Jason Coco
Use NSLog() to dump contents of objects. For example:
使用 NSLog() 转储对象的内容。例如:
NSData* myData = //... assume this exists
NSLog(@"Contents of myData: %@", myData);
NSLog has a printf-style format string (expects an NSString object) followed by a variable list of parameters, just like printf. The replacement character %@ represents an object the description method on an object. This is useful for dumping most Objective-C objects in Cocoa.
NSLog 有一个 printf 风格的格式字符串(需要一个 NSString 对象),后跟一个变量列表,就像 printf 一样。替换字符 %@ 代表一个对象,一个对象的描述方法。这对于在 Cocoa 中转储大多数 Objective-C 对象很有用。
If you want to dump the contents of an object using gdb (I see you tagged this with gdb), use the special "po" directive instead of print. For example:
如果您想使用 gdb 转储对象的内容(我看到您用 gdb 标记了它),请使用特殊的“po”指令而不是打印。例如:
gdb) po myData
will cause gdb to dump the myData object. po is a shortcut for print-object.
将导致 gdb 转储 myData 对象。po 是打印对象的快捷方式。
回答by diciu
Be careful with NSLog logging -> you most likely don't want it in production code.
小心 NSLog 日志记录 -> 您很可能不希望在生产代码中使用它。
You may want to use an alternate logging function that calls NSLog when your product is running in debug mode.
当您的产品在调试模式下运行时,您可能希望使用调用 NSLog 的备用日志记录功能。
回答by TCB13
I usualy go with this to "debug" NSArray contents:
我通常用它来“调试” NSArray 内容:
NSEnumerator *arrenum = [myarray objectEnumerator];
id cobj;
while ( cobj = [arrenum nextObject] ) {
NSLog(@"%@", cobj);
}
The code will enumerate all objects in the NSArray myarray, and then iterate through and print every object.
代码将枚举 NSArray 中的所有对象myarray,然后遍历并打印每个对象。
Hope this can be useful for someone!
希望这对某人有用!

