【问题标题】:How can I get the value / keys of NSDictionary objects in the debugger console?如何在调试器控制台中获取 NSDictionary 对象的值/键?
【发布时间】:2012-08-13 05:31:34
【问题描述】:

我设置了一个断点...

如果我这样做:

(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

我做错了什么?

【问题讨论】:

  • 您做错的第一件事就是将此问题标记为“xcode”。

标签: objective-c ios lldb


【解决方案1】:
error: no known method '-objectForKey:'; cast the message send to the method's return type

因此,它告诉您它不能仅从发送的消息名称推断返回类型信息——这很好。它甚至会告诉您如何准确地解决该问题 - 您必须cast将消息发送到方法的返回类型

打开 Apple 的文档,我们发现 - [NSDictionary objectForKey:] 返回 id - 通用的 Objective-C 对象类型。转换为 id (或者更好的是,如果您知道字典包含哪些类型的对象,则转换为该确切的对象类型)就可以了:

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

【讨论】:

  • 感谢你聪明的自己! :) 我将留下这个问题的另一个例子:失败: print [[[self.collectionView gestureRecognizers] objectAtIndex:0] isKindOfClass:[UITapGestureRecognizer class]] 好: print (BOOL)[[[self.collectionView gestureRecognizers] objectAtIndex:0] isKindOfClass:(Class)[UITapGestureRecognizer class]] 请注意使其工作所需的 2 次强制转换。
  • 把它留给Objective C,让简单的东西摆脱愚蠢的简单。
【解决方案2】:

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

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

po [[self dictionary] allKeys]

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      为什么不干呢

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

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

      【讨论】:

      • 我认为他试图从控制台而不是源代码获取信息。但是,在我看来,这是更好的方法。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2014-03-16
        • 1970-01-01
        • 2014-01-15
        • 1970-01-01
        • 2012-02-15
        • 1970-01-01
        • 2015-11-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多