2012-09-25 34 views
0

我有一個結構數組像下面數組subsubdictionaries內搜索到返回父詞典(目標C)

(
     { 
     id = 0; 
     name = "A"; 
     tables =   (
         { 
       comment = ""; 
       id = 0; 
       name = "T1"; 
      }, 
         { 
       comment = ""; 
       id = 1; 
       name = "T2"; 
      }, 
         { 
       comment = ""; 
       id = 4; 
       name = "T3"; 
      } 
     ); 
    }, 
     { 
     id = 1; 
     name = "B"; 
     tables =   (
         { 
       comment = ""; 
       id = 5; 
       name = "T1"; 
      }, 
         { 
       comment = ""; 
       id = 6; 
       name = "T2"; 
      } 
     ); 
    } 
) 

的一個考慮,我知道其中的一個詞典的ID鍵的值(如以id = 6),我怎麼能得到它的父母字典或更加精確的名字鍵值(在這種情況下B)。

我試過使用謂詞字符串,但問題是父字典也有一個名爲id的鍵,所以我得到了錯誤的結果。

編輯:ID密鑰是唯一的(並且也沒有必要在第一詞典中的元素的標識具有比具有較高索引字典的那些低級intiger值)

+0

是否所有字典中的鍵都是唯一的?如果沒有,那麼你不是在尋找一個字典,但可能是多個字典。 – waldrumpus

+0

是的,它們是獨一無二的。 – smihael

回答

0

嘗試此,其中陣列是您的結構化陣列

NSDictionary *parentDict=[[NSDictionary alloc]initWithDictionary:nil]; 
NSString *name=[[NSString alloc]init]; 
NSArray *table=[[NSArray alloc]initWithArray:nil]; 
for (NSDictionary * dict in array) { 
    table=[dict objectForKey:@"tables"]; 
    for (NSDictionary *childDict in table) { 
     if ([[childDict objectForKey:@"id"]isEqualToString:@"6"]) { 
      //if your id value is int then use ==6 to check 
      parentDict=dict; 
      name=[dict objectForKey:@"name"]; 
      return;     
     } 
    } 
}