Brian
Brian

Reputation: 7146

Viewing NSData contents in Xcode

I am running Xcode and I would like to dump out a NSData*. The variable in question is buffer. Is there a way to do this through the UI or the GDB debugger?

This is what I see at runtime

Edit

I've moved my notes into an answer.

Upvotes: 28

Views: 22884

Answers (13)

Ajith Madhan
Ajith Madhan

Reputation: 23

For Swift, use this debugger command:

po String(data: buffer, encoding: .utf8)

Upvotes: 1

Brett
Brett

Reputation: 4391

Xcode 11.4 (and probably earlier) supports examining the raw bytes of (NS)Data in the visual debugger. Set a breakpoint after the Data has been assigned, hover over the symbol until the popup appears, and click on the eye or info icon. Roll for dexterity to prevent the popup from closing on you by moving outside of the tiny target.

Tested with Swift but presumably works with C family languages, too.

(Note: the eye icon produces the output below which is nicely formatted but, uh... seems to be missing the final column of bytes?)

Xcode data view

Upvotes: 1

algrid
algrid

Reputation: 5954

For Swift code I'm doing the following:

When my program is stopped in a breakpoint I use the "Add expression" option and enter [UInt8](data) where data is my Data instance:

enter image description here

After that I can see the data contents:

enter image description here

Upvotes: 0

Brian
Brian

Reputation: 7146

Unfortunately, none of the suggestions so far solved the problem of actually being able to quickly display the data inside NSData.

I wrote a simple method that works the way I need it to. From the GDB window, I can type in print [Util dumpData:theData] and I will get nice, formatted output.

+(void) dumpData:(NSData *)data
{
    unsigned char* bytes = (unsigned char*) [data bytes];

    for(int i=0;i< [data length];i++)
    {
        NSString* op = [NSString stringWithFormat:@"%d:%X",i,bytes[i],nil];
        NSLog(@"%@", op);
    }
}

NsLog Output

0:24
1:0
2:4
3:0
4:0
5:0

Upvotes: 4

Max MacLeod
Max MacLeod

Reputation: 26652

In Swift this should do the trick:

po String(data:buffer!, encoding: NSUTF8StringEncoding)

Upvotes: 9

GarMan
GarMan

Reputation: 1044

No one has ever correctly answered the question. After 2 years I think it's time for one :)

Assuming you have in your code

NSData* myData;

Then in lldb you type

me read `[myData bytes]` -c`[myData length]`

If the format of the dump is not to your liking you can add '-t ' for example

me read `[myData bytes]` -c`[myData length]` -t int

For more help type

help me read

in lldb

Upvotes: 41

quickshiftin
quickshiftin

Reputation: 69621

The easiest way for me (during local development only!) is to convert to unused NSString instances. Then the values show right up in the debugger. Once I'm finished, I nuke those lines of code.

From this old thread

NSString* newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

enter image description here

Upvotes: 0

Kevin Clifton
Kevin Clifton

Reputation: 509

From Xcode 5 (lldb), you can use the following:

po (NSString *)[[NSString alloc] initWithData:buffer encoding:4]

Note that this assumes your NSData instance is encoded with NSUTF8StringEncoding, but you can look up the other values in the headers or the documentation.

So if you're debugging something like a JSON request that's wrapped up in an NSURLSessionDataTask, the request data is in task.originalRequest.httpBody, and you can view that in the debugger with

po (NSString *)[[NSString alloc] initWithData:task.originalRequest.HTTPBody encoding:4]

Upvotes: 24

Troy
Troy

Reputation: 5399

In lldb, the following works to let you examine the contents of NSData objects:

You can get the address of the bytes for use with various debugger commands like this:

p (void *)[buffer bytes]

You see something like this:

(void *) $32 = 0x0b5e11f0

If you know the underlying data is a string, you can do this:

p (char *)[buffer bytes]

and the debugger will output:

(char *) $33 = 0x0b5e11f0 "This is the string in your NSData, for example."

Upvotes: 16

pasawaya
pasawaya

Reputation: 11595

I posted this as an answer to this relevant question:

Once you place a breakpoint, run, and the program stops at the breakpoint, hover your cursor over the variable/value you want to see like this:

enter image description here

You could also place an NSLog(@"%@", yourLabel.text); to view the contents of that label/other object type.

One other option is to run GDB in the console like this:

gdb
attach <your process name>

And then use the po (print-object) command to view the value of a variable like this:

po variableName

To view the value of primitive types (int, float, long, double, char, etc.), you can just use the print command while running GDB in the console like this:

print yourPrimitiveVariable

Hope this helps!

EDIT:

With the po command, you can print out the value of an object using both the property name (self.myProperty) or the ivar name (possibly _myProperty). I demonstrate this here:

enter image description here

Upvotes: 1

ratbum
ratbum

Reputation: 1023

I think I have it now.

Right click on NSData in the list displayed there, and click 'Show Memory Of "x"'.

Upvotes: 1

Anshu Chimala
Anshu Chimala

Reputation: 2800

Right click buffer and click on Print description of "buffer".

The console should say

Printing description of buffer:
<your data here ...>

Upvotes: 6

jscs
jscs

Reputation: 64002

Your data instance is empty.

It wouldn't only display the address otherwise. -[NSData description] includes a printout of the contents of the data. The bytes are grouped in fours and printed in hex with a leading 0 placeholder:

char arr[] = {0x1, 0x2, 0xA, 0x4, 0xF};
NSData * dat = [NSData dataWithBytes:arr length:5];
NSLog(@"%@", dat);

2012-07-17 22:24:48.973 PrintDat[61264:403] <01020a04 0f>

Using po dat at the debugger's command line will give you the same results, including the address:

(NSData *) $1 = 0x00007f98da500180 <01020a04 0f>

The contextual menu route that Anshu suggested also uses the description method.

Upvotes: 1

Related Questions