2016-07-22 99 views
0

有人可以幫助我如何通過下面的數據進行判斷並檢索鍵「文本」的值。起初,我有以下數據使用字典鍵通過嵌套數組NSPredicate

{ 
    ArrayName1 =  (
       { 
      Target = "<null>"; 
      Text = "Name1"; 
      Value = 1; 
     }, 
       { 
      Target = "<null>"; 
      Text = "Name2"; 
      Value = 2; 
     } 
    ); 
    ArrayName2 =  (
       { 
      Target = "<null>"; 
      Text = "Name3"; 
      Value = 3; 
     }, 
       { 
      Target = "<null>"; 
      Text = "Name4"; 
      Value = 4; 
     } 
    ); 
    ArrayName3 =  (
       { 
      Target = "<null>"; 
      Text = "Name5"; 
      Value = 5; 
     }, 
       { 
      Target = "<null>"; 
      Text = "Name6"; 
      Value = 6; 
     } 
    ); 
    ArrayName4 =  (
       { 
      Target = "<null>"; 
      Text = "somename"; 
      Value = somevalue; 
     } 
    ); 
    ArrayName4 =  (
       { 
      Target = "<null>"; 
      Text = "somename"; 
      Value = "somevalue"; 
     } 
    ); 
} 

我想最終的結果將被存儲在「resultarray」這應該由搜索字符串從所有的陣列包含的「文本」的鍵值字典,也想存儲相同數組中「Value」鍵的相應值。

感謝, 普拉迪普

+0

顯示你有嘗試過嗎?並且可以請你在搜索結束後舉例說明你想要的內容嗎? –

+0

發佈你到目前爲止嘗試過的代碼..? – remyr3my

+0

@ Dev.RK,請查看下面我試過的方法。 – user3310076

回答

0

@ Dev.RK @ Cyph3r - 下面是我試過的方法。 @ Dev.RK,@ Cyph3r。我嘗試了下面的方法,包括複合謂詞。

//Method 1 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K.%K CONTAINS[c] %@", @"Practice",@"Text",searchTextString]; 
searchArray = [[quickSearchDataDict allValues] filteredArrayUsingPredicate:predicate]; 


//Method 2 
NSPredicate *predicateArray1 = [NSPredicate predicateWithFormat:@"(TEXT==%@)",searchTextString]; 
    NSPredicate * predicateArray2 = [NSPredicate predicateWithFormat:@"SELF.%K.%K contains[c] %@",@"ArrayName2",@"Text",searchTextString]; 
    NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateArray1,predicateArray2]]; 
searchArray = [[quickSearchDataDict allValues] filteredArrayUsingPredicate:predicate]; 
0

試試這個,希望這能解決您的problem-

NSMutableDictionary *dict=[[NSMutableDictionary alloc]init]; 

for (int i=0; i<5; i++) { 
    NSMutableArray *ary=[[NSMutableArray alloc]init]; 
    for (int j=0; j<2; j++) { 
     NSMutableDictionary *dic=[[NSMutableDictionary alloc]init]; 
     [dic setValue:[NSString stringWithFormat:@"none"] forKey:@"Target"]; 
     [dic setValue:[NSString stringWithFormat:@"tst%d",j] forKey:@"Text"]; 
     [dic setValue:[NSString stringWithFormat:@"%d",j+i] forKey:@"Value"]; 
     [ary addObject:dic]; 
    } 
    [dict setObject:ary forKey:[NSString stringWithFormat:@"ArrayName%d",i]]; 
} 

NSArray *searchary=[dict valueForKey:[NSString stringWithFormat:@"%@",@"ArrayName0"]]; 

NSPredicate *pre0 = [NSPredicate predicateWithFormat:@"SELF.%K contains[cd] %@",@"Text", @"tst"]; 

NSArray *resultArray= [searchary filteredArrayUsingPredicate:pre0]; 

NSLog(@"%@",resultArray); 

這裏是對數

1.文本== TST0

(
     { 
     Target = none; 
     Text = tst0; 
     Value = 0; 
    } 
) 

2 。Text == tst

(
     { 
     Target = none; 
     Text = tst0; 
     Value = 0; 
    }, 
     { 
     Target = none; 
     Text = tst1; 
     Value = 1; 
    } 
) 

,這是完整的數據 -

{ 
    ArrayName0 =  (
       { 
      Target = none; 
      Text = tst0; 
      Value = 0; 
     }, 
       { 
      Target = none; 
      Text = tst1; 
      Value = 1; 
     } 
    ); 
    ArrayName1 =  (
       { 
      Target = none; 
      Text = tst0; 
      Value = 1; 
     }, 
       { 
      Target = none; 
      Text = tst1; 
      Value = 2; 
     } 
    ); 
    ArrayName2 =  (
       { 
      Target = none; 
      Text = tst0; 
      Value = 2; 
     }, 
       { 
      Target = none; 
      Text = tst1; 
      Value = 3; 
     } 
    ); 
    ArrayName3 =  (
       { 
      Target = none; 
      Text = tst0; 
      Value = 3; 
     }, 
       { 
      Target = none; 
      Text = tst1; 
      Value = 4; 
     } 
    ); 
    ArrayName4 =  (
       { 
      Target = none; 
      Text = tst0; 
      Value = 4; 
     }, 
       { 
      Target = none; 
      Text = tst1; 
      Value = 5; 
     } 
    ); 
} 
+0

感謝您寶貴的時間。我能夠將字典分隔成多個數組並通過每個數組進行判斷。但是我想要爲所有數組完成謂詞,並將匹配字符串的鍵值對組合在一個數組中。 – user3310076

+0

意味着你不想要特定的數組來判斷ArrayName0嗎? –

+0

我希望'dict'中的所有數組都被預測,並且匹配應該在一個數組中。我做了一些替代,將所有數組(ArrayName1,ArrayName2等)合併到一個數組中並進行預測。不過,如果你爲我的意圖找到解決辦法,請告訴我。 – user3310076