请原谅我在这里提出了一个可能很愚蠢的问题,但是在其他编程语言(PHP或Perl等脚本语言)中,通常很容易转储变量中包含的所有内容.
例如,在PHP中有var_dump()
或print_r()
个函数.Perl有Data::Dumper
CPAN类等等.
对于Objective-C有类似的东西吗?在某些情况下,可以像这样转储所有内容,而不是使用gdb判断每个变量,这将非常方便.
请原谅我在这里提出了一个可能很愚蠢的问题,但是在其他编程语言(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 description
或 debugDescription
methods to return anything you want. If not, you could override the description
或 debugDescription
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]
.