2012-07-04 32 views
3
解析的NSDictionary

我有以下的plist:例外內的NSDictionary

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>1_NARR</key> 
    <dict> 
     <key>audio</key> 
     <string>1 NARR AUDIO LOCATION</string> 
     <key>text</key> 
     <string>1 NARR TEXT</string> 
    </dict> 
    <key>1_C1_1</key> 
    <dict> 
     <key>text</key> 
     <string>1 C1 1 TEXT</string> 
     <key>audio</key> 
     <string>1 C1 1 AUDIO LOCATION</string> 
     <key>position</key> 
     <string>1 C1 1 Position</string> 
     <key>frameX</key> 
     <string>1 C1 1 FRAMEX</string> 
    </dict> 
</dict> 
</plist> 

這裏是我的代碼在列表中循環:

NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dialogues" ofType:@"plist"]]; 

    if (mainDictionary != nil){ 
     NSLog(@"viewDidLoad: mainDictionary != nil"); 
     [self parseDictionary:mainDictionary]; 
    } 

- (void)parseDictionary:(NSDictionary*)aDictionary 
{ 
    // enumerate key/values, recursing as necessary 
    [aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { 
     if ([value isKindOfClass: [NSDictionary class]]) { 
      NSLog(@"parseDictionary: isKindOfClass: NSDictionary"); 
      [self parseDictionary: value]; 
     } else { 
      //... parse value here ... 
      NSLog(@"parseDictionary: else: "); 
      if ([value valueForKey:@"text"]) 
      {    
       NSLog(@"parseDictionary: else: if: "); 
       //NSLog(@"parseDictionary: if: valueForKey:text == %@", [value valueForKey:@"text"]); 
      } 
     } 
    }]; 
} 

我得到以下異常:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x6899760> valueForUndefinedKey:]: this class is not key value coding-compliant for the key text.' 

我無法弄清楚爲什麼?你看到有關邏輯的任何問題嗎?希望你的迴應。

下面是完整的堆棧跟蹤:

Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<__NSCFString 0x6e7a780> valueForUndefinedKey:]: this class 
is not key value coding-compliant for the key text.' 
*** First throw call stack: (0x16c0022 0x14e0cd6 0x16bfee1 0x1e7efe 0x156831 0x155c99 0x28bf 0x16bf8f8 0x15e39ed 0x165bbd6 0x165b84d 
0x165b795 0x27cb 0x287e 0x16bf8f8 0x15e39ed 0x165bbd6 0x165b84d 
0x165b795 0x27cb 0x2a41 0x55fa1e 0x23f1 0x4964be 0x497274 0x4a6183 
0x4a6c38 0x49a634 0x1be3ef5 0x1694195 0x15f8ff2 0x15f78da 0x15f6d84 
0x15f6c9b 0x496c65 0x498626 0x201d 0x1f95) terminate called throwing 
an exception 
+0

請張貼堆棧跟蹤。 – trojanfoe

+0

調試101 - 你有沒有嘗試單步執行代碼? –

回答

3

考慮您的plist的這一部分:

<key>audio</key> 
    <string>1 NARR AUDIO LOCATION</string> 

在你的塊,key將字符串 「音頻」,並value將是字符串「1 NARR AUDIO LOCATION」。該值是一個字符串,而不是一個字典,所以我們在條件中點擊了else子句。然後,你這樣做:

 if ([value valueForKey:@"text"]) 

但是,正如我們已經發現,value是一個字符串。您不能在字符串上調用字典方法。

+0

謝謝。我現在明白了我的代碼的問題。我怎樣才能解決它通過傳遞鍵來獲取值? – Alex

+0

@Alex'價值'已經是價值。 – 2012-07-04 13:18:01

1

您試圖在NSString對象上使用鍵值編碼API方法valueForKey:reference)。

你似乎有你的邏輯後至前,因爲我認爲你正在嘗試一個NSDictionary,但內訪問text鍵時,值一個NSDictionary例如在else您正在執行它(即部分測試)。

注意:無論如何,請使用[NSDictionary objectForKey:]

編輯:試試這個版本:

- (void)parseDictionary:(NSDictionary*)aDictionary 
{ 
    // enumerate key/values, recursing as necessary 
    [aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) { 
     if ([value isKindOfClass: [NSDictionary class]]) { 
      NSLog(@"parseDictionary: isKindOfClass: NSDictionary"); 
      [self parseDictionary: value]; 
     } else { 
      NSLog(@"'%@'='%@'", key, value); 
     } 
    }]; 
} 
+0

謝謝。我現在明白了我的代碼的問題。我怎樣才能解決它通過傳遞鍵來獲取值? – Alex

+0

@Alex我用一個'printDictionary'版本更新了我的答案,它應該*打印每個字典中的所有關鍵字。 – trojanfoe