Viewing NSData contents in Xcode

前端 未结 12 1973
我在风中等你
我在风中等你 2021-01-31 09:51

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?

相关标签:
12条回答
  • 2021-01-31 10:27

    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

    0 讨论(0)
  • 2021-01-31 10:28

    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

    0 讨论(0)
  • 2021-01-31 10:28

    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:

    After that I can see the data contents:

    0 讨论(0)
  • 2021-01-31 10:30

    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."
    
    0 讨论(0)
  • 2021-01-31 10:31

    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

    0 讨论(0)
  • 2021-01-31 10:34

    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.

    0 讨论(0)
提交回复
热议问题