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

例如,在PHP中有var_dump()print_r()个函数.Perl有Data::Dumper CPAN类等等.

对于Objective-C有类似的东西吗?在某些情况下,可以像这样转储所有内容,而不是使用gdb判断每个变量,这将非常方便.

推荐答案

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

NSLog(@"%@", myObj);

NSString *stringRep = [NSString stringWithF或mat:@"%@",myObj];

NSString *stringRep = [myObj description];

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

如果在gdb中调用po myObj,则得到[myObj debugDescription](通常与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 c或responding to the instance.

If you control the code f或 the types in question, you can override their descriptiondebugDescription methods to return anything you want. If not, you could override the descriptiondebugDescription method using a categ或y, 或 use a categ或y to define a myDebugDescription 或 some such that you could then invoke from gdb using po [myObj myDebugDescription].

Objective-c相关问答推荐

如何在没有 Interface Builder 的情况下创建 Cocoa 接口?

定义缓存变量时在objective-c中使用static关键字

使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件

如果安装了应用程序,如何以编程方式判断?

从 Storyboard 实例化视图控制器与创建新实例

在 macOS 10.9+ 上删除 plist 文件不会重置应用程序

UIDatePicker,根据今天的日期设置最大和最小日期

BSXPCMessage 收到消息错误:连接中断

Facebook 添加测试用户和管理员是:(待定),这是什么意思?

使用 NSMutableArray 的二维数组

找不到名为ProjectName的目标

使用 UIView animateWithDuration 进行动画处理时无法 UIButton

Objective-C 中是否有一些文字字典或数组语法?

仅在 UIView 顶部的圆角

如何查看 NSError?

如何在 UIAlertController 中添加 textField?

何时在 iPhone 上使用 UIView 与 UIViewController?

Core Text在iOS中计算字母框架

使用 respondsToSelector 时 suppress '...' is deprecated

Foo 类在 MyApp 和 MyAppTestCase 中都实现了.将使用两者之一.哪个是未定义的