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

Objective c 如何在调试器控制台中获取NSDictionary对象的值/键?,objective-c,ios,lldb,Objective C,Ios,Lldb,我设置了一个断点 如果我这样做: (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

我设置了一个断点

如果我这样做:

(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
我做错了什么?

为什么不直接做呢

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


lldb命令print要求要打印的值是非对象。打印对象时应使用的命令是po

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

po [[self dictionary] allKeys]
因此,它告诉您不能仅仅从发送的消息的名称推断返回类型信息,这很好。它甚至告诉您必须如何准确地解决这个问题——您必须将消息send转换为方法的返回类型

打开苹果的文档,我们发现
-[NSDictionary objectForKey:
返回
id
——通用的Objective-C对象类型。转换为id(或者更好,如果您知道字典中包含的对象类型,则转换为确切的对象类型)可以实现以下技巧:

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

要在GDB或LLDB中打印对象的
说明
,需要使用或
po

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

目前lldb中似乎存在一个bug,导致
po dictionary[@“key”]
打印一个空行,而不是键的值。使用
[dictionary[@“key”]description]
来获取值。

你做的第一件错事是将这个问题标记为“xcode”。我认为他试图从控制台获取信息,而不是源代码。不过,在我看来,这是更好的方式。我感谢你聪明的aleck self!:)我将留下这个问题的另一个例子:失败:
print[[self.collectionView gesturecognizers]objectAtIndex:0]是类的种类:[UITapGestureRecognizer类]]
Good:
print(BOOL)[[self.collectionView gesturecognizers]objectAtIndex:0]是类的种类:[UITapGestureRecognizer类]]
请注意使其工作所需的两种类型转换。将简单从愚蠢的简单中剔除出来,留给目标C。
(lldb) print (MyObject *)[(NSDictionary *)[self dictionary] objectForKey:@"foobar"]
(lldb) po [self dictionary]
(lldb) po [[self dictionary] objectForKey:@"foobar"]