2012-08-16 58 views
11

我设置一个断点...如何在调试器控制台中获取NSDictionary对象的值/键?

如果我这样做:

(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 

我在做什么错?

+1

你做错了第一件事就是将这个问题标记为'xcode'。 – 2012-08-16 19:51:03

+0

'po [self dictionary]' – Joe 2012-08-16 19:54:01

回答

14

你会说英语吗? - 看起来你做得很好!而啊,真巧,调试器也行!

非常好,我们完成了困难的一部分。所以,现在你了解对方与调试器,让我们看看它表明:

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

所以,它告诉你不能从消息发送的名称只是推断返回类型的信息 - 这是完全正常的(一个不使用匈牙利符号,对吧?)。它甚至会告诉你如何解决这个问题 - 你必须转换消息发送到该方法的返回类型

启动Apple的文档,我们发现- [NSDictionary objectForKey:]返回id - 通用Objective-C对象类型。铸造于ID(甚至更好,如果你知道什么类型的字典持有的对象,铸造,准确的对象类型)的伎俩:

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

我感谢你的聪明的自我! :)我会留下这个问题的另一个例子: **失败:**'print [[[self。我们可以通过下面的例子来说明如何使用这个方法来创建一个新的对象:[ObjectAtIndex:0] isKindOfClass:[UITapGestureRecognizer class]]' **好:**'print(BOOL)[[[self.collectionView gestureRecognizers] objectAtIndex:0] isKindOfClass:(Class)[UITapGestureRecognizer class]请注意需要2个演员才能使其工作。 – Jeff 2013-07-02 14:18:49

+0

将它留在Objective C中,以简单的方式取得简单。 – 2016-11-04 20:52:59

3

为什么不只是做

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

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

我想他试图从控制台获取信息而不是源代码。然而,在我看来,这是更好的方式。 – 2012-08-16 19:57:48

13

的LLDB命令打印预计要打印的值是一个非对象。您应该用来打印对象的命令是po。

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

po [[self dictionary] allKeys] 
3

要打印您需要在GDB或LLDB对象的description使用print-objectpo

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

似乎是导致po dictionary[@"key"]打印一个空行,而不是关键的价值目前在LLDB的错误。改用[dictionary[@"key"] description]来获得该值。

相关问题