Viewing NSData contents in Xcode

前端 未结 12 1975
我在风中等你
我在风中等你 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:36

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

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

    The console should say

    Printing description of buffer:
    <your data here ...>
    
    0 讨论(0)
  • 2021-01-31 10:40

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

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

    I think I have it now.

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

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

    In Swift this should do the trick:

    po String(data:buffer!, encoding: NSUTF8StringEncoding)
    
    0 讨论(0)
  • 2021-01-31 10:49

    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

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