2011-03-09 112 views
0

在嘗試嘗試100次嘗試後,即刻訴諸於SO。從嵌套的NSDictionary中獲取值

我使用字典這個輸出HTML解析器的libxml2

{ 
     nodeChildArray =    (
          { 
       nodeAttributeArray =      (
              { 
         attributeName = class; 
         nodeContent = imgContainer; 
        } 
       ); 
       nodeChildArray =      (
              { 
         nodeAttributeArray =        (
                  { 
           attributeName = src; 
           nodeContent = "/~/media/Images/Image 2.ashx"; 
          }, 
                  { 
           attributeName = alt; 
           nodeContent = "Image 2"; 
          }, 
                  { 
           attributeName = width; 
           nodeContent = 60; 
          }, 
                  { 
           attributeName = height; 
           nodeContent = 112; 
          } 
         ); 
         nodeName = img; 
        } 
       ); 
       nodeName = div; 
      } 
     ); 
     nodeName = td; 
    }, 

我試圖讓nodeContent是/〜/媒體/圖片/圖像2.ashx的...

任何想法我的迭代應該如何?

+0

我也想知道如何在這種情況下使用NSDictionary valueForKeyPath的方法...如果有可能? – xoail 2011-03-10 03:12:45

回答

1

您的訪問代碼看起來應該像

[[[nodeChildArray objectAtIndex:0] objectAtIndex:0] valueForKey:@"nodeContent"]; 

我使用0您可以從循環運行高達陣列的數量得到它。

請確保你的數組有對象或做出適當的if else結構並將其分解爲單獨的語句。那麼它應該看起來像

if([nodeChildArray count] >0) 
{ 
array1=[nodeChildArray objectAtIndex:0]; //an array object 
if([array1 count]>0) 
{ 
dictionary=[array1 objectAtIndex:0]; //a dictionary 
//then you can find the value 

id value=[dictionary valueForKey:@"nodeContent"]; 
} 
} 
+0

謝謝,但我有一個警告說NSDictionary可能不會響應objectAtIndex。它也沒有執行。這是我如何嘗試:NSArray * objData = [self PerformHTMLXPathQuery:responseData queryString:xpathQueryString]; [responseData release]; \t爲(*的NSDictionary在objData維生素) \t { \t \t的NSLog(@ 「%@」,[[[維生素objectAtIndex:0] objectAtIndex:0] valueForKey:@ 「nodeContent」]); – xoail 2011-03-10 02:38:35

+0

ok然後你有數組字典,那麼你可以通過valueForKey除objectAtIndex; – Ishu 2011-03-10 03:52:23

+0

這是有效的,如果你確信這個層次:'[[[dict valueForKey:@「foo」] valueForKey:@「man」] valueForKey:@「chu」]' – cleverbit 2012-08-30 17:23:10