簡體   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