How to dump data stored in objective-c object (NSArray or NSDictionary)

后端 未结 5 815
情深已故
情深已故 2020-12-08 00:41

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

5条回答
  •  孤街浪徒
    2020-12-08 01:12

    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);
    

    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 NSObject for the purpose of printing a description (not a dump) of an object.

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

    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.

    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].

提交回复
热议问题