2016-07-22 105 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