在 Xcode 中查看 NSData 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11531353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:55:35  来源:igfitidea点击:

Viewing NSData contents in Xcode

objective-cxcode

提问by Brian

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?

我正在运行 Xcode,我想转储一个 NSData*。有问题的变量是缓冲区。有没有办法通过 UI 或 GDB 调试器来做到这一点?

This is what I see at runtime

这是我在运行时看到的

Edit

编辑

I've moved my notes into an answer.

我已将笔记移至答案中。

采纳答案by Brian

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

不幸的是,到目前为止,所有建议都没有解决实际能够在 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.

我写了一个简单的方法,它按照我需要的方式工作。在 GDB 窗口中,我可以输入print [Util dumpData:theData],我会得到漂亮的格式化输出。

+(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:24
1:0
2:4
3:0
4:0
5:0

回答by GarMan

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

从来没有人正确回答过这个问题。2 年后,我认为是时候了 :)

Assuming you have in your code

假设你的代码中有

NSData* myData;

Then in lldb you type

然后在 lldb 中输入

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

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

如果转储的格式不符合您的喜好,您可以添加“-t”,例如

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

For more help type

更多帮助类型

help me read

in lldb

在 lldb 中

回答by Kevin Clifton

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

从 Xcode 5 (lldb),您可以使用以下内容:

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.

请注意,这假设您的 NSData 实例使用 编码NSUTF8StringEncoding,但您可以在标题或文档中查找其他值。

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

因此,如果您正在调试类似包含在 中的 JSON 请求,NSURLSessionDataTask请求数据在 中task.originalRequest.httpBody,您可以在调试器中查看它

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

回答by Troy

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

在 lldb 中,以下工作可以让您检查 NSData 对象的内容:

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

您可以获得用于各种调试器命令的字节地址,如下所示:

p (void *)[buffer bytes]

You see something like this:

你会看到这样的事情:

(void *)  = 0x0b5e11f0

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

如果您知道基础数据是string,则可以执行以下操作:

p (char *)[buffer bytes]

and the debugger will output:

调试器将输出:

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

回答by Anshu Chimala

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

右键单击buffer并单击Print description of "buffer"

The console should say

控制台应该说

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

回答by Max MacLeod

In Swift this should do the trick:

在 Swift 中,这应该可以解决问题:

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

回答by jscs

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:

否则它不仅会显示地址。-[NSData description]包括数据内容的打印输出。字节按四位分组并以十六进制打印,并带有前导 0 占位符:

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>

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

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

使用po dat在调试程序的命令行会给你同样的结果,包括地址:

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

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

The contextual menu route that Anshu suggested also uses the descriptionmethod.

安舒建议的上下文菜单路径也使用了该description方法。

回答by ratbum

I think I have it now.

我想我现在有了。

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

右键单击显示在那里的列表中的 NSData,然后单击“显示“x”的内存”。

回答by pasawaya

I posted this as an answer to thisrelevant 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.

您还可以放置NSLog(@"%@", yourLabel.text);以查看该标签/其他对象类型的内容。

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

另一种选择是在控制台中运行 GDB,如下所示:

gdb
attach <your process name>

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

然后使用po(print-object) 命令查看变量的值,如下所示:

po variableName

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

要查看原始类型(int, float, long, double, char, 等)的值,您可以print在控制台中运行 GDB 时使用该命令,如下所示:

print yourPrimitiveVariable

Hope this helps!

希望这可以帮助!

EDIT:

编辑:

With the pocommand, 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:

使用该po命令,您可以使用属性名称 ( self.myProperty) 或 ivar 名称(可能_myProperty)打印出对象的值。我在这里演示:

enter image description here

在此处输入图片说明

回答by quickshiftin

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

对我来说最简单的方法(仅在本地开发期间!)是转换为未使用的NSString实例。然后这些值会直接显示在调试器中。完成后,我会核对这些代码行。

From this old thread

从这个旧线程

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

enter image description here

在此处输入图片说明