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

65,983

Solution 1

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

Solution 2

you can also use the gdb print object command to quickly view an object in the debugger:

po dictionary

This will be basically the same as calling NSLog(...) from within your code.

Also useful when printing out NSData that contains ASCII data is:

p (char *) [data bytes]

Solution 3

Use NSLog() to dump contents of objects. For example:

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.

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) po myData

will cause gdb to dump the myData object. po is a shortcut for print-object.

Solution 4

Be careful with NSLog logging -> you most likely don't want it in production code.

You may want to use an alternate logging function that calls NSLog when your product is running in debug mode.

Solution 5

I usualy go with this to "debug" NSArray contents:

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.

Hope this can be useful for someone!

Share:
65,983
jpm
Author by

jpm

I have my own company (Ipanema Labs LLC - http://ipanemalabs.com) working as a freelance Web/iOS/Android developer. Tons of experience in web development with the LAMP stack, especially PHP and MySQL backends. Organizer of the Houston PHP Users Group meetup since 2004. Founder and organizer of the Houston iPhone Developers meetup. Book author in a previous life ( http://smartybook.com ) -- never again.

Updated on July 09, 2022

Comments

  • jpm
    jpm almost 2 years

    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.

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

    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.

  • Barry Wark
    Barry Wark over 15 years
    The string formatted representation (used by NSLog and [NSString stringWithFormat:] et al.) may not be a full "dump" of the object. It is the result of the object's description method. Since objects can override this description, you may varying results from its use.
  • Jason Coco
    Jason Coco over 15 years
    I know, which is why I said "This is useful for dumping /most/ Objective-C objects in Cocoa" but it is basically what the OP is asking for.
  • philsquared
    philsquared over 15 years
    This is a good point. I lot of new Objective C programmers don't realise this. If you think of NSLog as being ObjC's printf or std::cout you shouldn't go far wrong (and writing a DLog wrapper macros is trivial).
  • Manuel
    Manuel over 12 years
    the description method is just what i need! Thanks for the information! :)
  • jamil ahmed
    jamil ahmed about 12 years
    This only prints the first few hundred bytes. How can I make it print all of them?
  • Duck
    Duck over 9 years
    You can use NSLog as much as you want. All you have to do is to add these lines on your .pch file, to magically strip all NSLog lines from release: #ifndef DEBUG #define NSLog(...) #endif ... see here
  • Jared Chu
    Jared Chu over 7 years
    Always get message like this: <ObjectName: 0x608000482530>