Viewing NSData contents in Xcode

前端 未结 12 2259
我在风中等你
我在风中等你 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]
    

提交回复
热议问题