繁体   English   中英

如何在调试器控制台中获取NSDictionary对象的值/键?

[英]How can I get the value / keys of NSDictionary objects in the debugger console?

我设定了一个断点......

如果我做:

(lldb) print [self dictionary]
(NSDictionary *) $5 = 0x0945c760 1 key/value pair

但如果我这样做:

(lldb) print [[self dictionary] allKeys]
error: no known method '-allKeys'; cast the message send to the method's return type
error: 1 errors parsing expression

即使我尝试访问我知道的密钥在那里..

(lldb) print [[self dictionary] objectForKey:@"foobar"]
error: no known method '-objectForKey:'; cast the message send to the method's return     type
error: 1 errors parsing expression

我究竟做错了什么?

error: no known method '-objectForKey:'; cast the message send to the method's return type

因此,它告诉您它不能仅仅从消息发送的名称推断出返回类型信息 - 这完全没问题。 它甚至会告诉你你究竟如何解决这个问题-你必须将消息发送到方法的返回类型

- [NSDictionary objectForKey:] Apple的文档,我们发现- [NSDictionary objectForKey:]返回id - 通用的Objective-C对象类型。 转换为id(或者甚至更好,如果你知道你的字典包含什么类型的对象,转换为确切的对象类型)就可以了:

(lldb) print (MyObject *)[(NSDictionary *)[self dictionary] objectForKey:@"foobar"]

lldb命令打印期望您要打印的值是非对象。 您应该用来打印对象的命令是po。

当你告诉lldb打印该值时,它会查找一个名为allKeys的方法,该方法返回一个非对象并失败。 请尝试以下命令...

po [[self dictionary] allKeys]

要在GDB或LLDB中print-objectdescription ,您需要使用print-objectpo

(lldb) po [self dictionary]
(lldb) po [[self dictionary] objectForKey:@"foobar"]

为什么不这样做呢

NSLog(@"dict: %@", dictionary);

要么

NSLog(@"dict objectForKey:foobar = %@", [dictionary objectForKey:@"foobar"]);

lldb中似乎有一个错误,导致po dictionary[@"key"]打印一个空行而不是键的值。 使用[dictionary[@"key"] description]来获取值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM