2011-03-09 115 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