如何转储存储在 Objective-C 对象(NSArray 或 NSDictionary)中的数据 [英] How to dump data stored in objective-c object (NSArray or NSDictionary)

查看:26
本文介绍了如何转储存储在 Objective-C 对象(NSArray 或 NSDictionary)中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我在这里提出一个可能很愚蠢的问题,但在其他编程语言(PHP 或 Perl 等脚本语言)中,通常很容易转储变量中包含的所有内容.

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 中有 var_dump()print_r() 函数.Perl 有 Data::Dumper CPAN 类等.

For instance, in PHP there are the var_dump() or print_r() functions. Perl has the Data::Dumper CPAN class, etc etc.

Objective-C 有类似的东西吗?在某些情况下,能够像这样转储所有内容会非常方便,而不是使用 gdb 来检查每个变量.

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.

推荐答案

在 Cocoa 中,没有像 PHP 的 print_r 或 python 的 repr 那样的转储",因为没有文本格式可以表示"像那些语言一样的对象.如果你使用

In Cocoa, there is no "dump" like PHP's print_r or python's repr since there is no textual format that "represents" an object as in those languages. If you use

NSLog(@"%@", myObj);

NSString *stringRep = [NSString stringWithFormat:@"%@",myObj];

NSString *stringRep = [myObj description];

你会得到(在第一种情况下登录到控制台),[myObj description] 的结果,NSObject 中定义的一个方法,用于打印 描述(不是转储)对象.

you will get (logged to console in the first case), the result of [myObj description], a method defined in NSObject for the purpose of printing a description (not a dump) of an object.

如果您在 gdb 中调用 po myObj,您会得到 [myObj debugDescription](通常与 description 相同,但并非总是如此).

If you invoke po myObj in gdb, you get [myObj debugDescription] (often the same as description, but not always).

NSArrayNSDictionaryNSData 这样的类会覆盖 description 以打印其内容的非常有用的递归描述,但默认的[NSObject description]只打印实例对应的指针值.

Classes like NSArray and NSDictionary and NSData override description to print a pretty useful recursive description of their contents, but the default [NSObject description] prints only the pointer value corresponding to the instance.

如果您控制相关类型的代码,则可以覆盖它们的 descriptiondebugDescription 方法以返回您想要的任何内容.如果没有,您可以使用类别覆盖 descriptiondebugDescription 方法,或使用类别来定义 myDebugDescription 或一些这样您可以然后使用 po [myObj myDebugDescription] 从 gdb 调用.

If you control the code for the types in question, you can override their description or debugDescription methods to return anything you want. If not, you could override the description or debugDescription method using a category, or use a category to define a myDebugDescription or some such that you could then invoke from gdb using po [myObj myDebugDescription].

这篇关于如何转储存储在 Objective-C 对象(NSArray 或 NSDictionary)中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆