使用字典键通过嵌套数组NSPredicate

时间:2016-07-22 12:22:20

标签: ios xcode nspredicate

有人可以帮助我如何通过以下数据进行谓词并检索键的值" Text"。最初我有以下数据的字典

{
    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";
        }
    );
}

我希望最终结果存储在&#34; resultarray&#34;其中应包含&#34; Text&#34;的值key来自所有数组的搜索字符串,并且还想存储&#34; Value&#34;的相应值。键在同一个数组中。

谢谢, 普拉迪普

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

试试这个,希望这会解决你的问题 -

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。文本== 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;
        }
    );
}