Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用谓词筛选词典_Iphone_Objective C_Nsdictionary_Nspredicate - Fatal编程技术网

Iphone 使用谓词筛选词典

Iphone 使用谓词筛选词典,iphone,objective-c,nsdictionary,nspredicate,Iphone,Objective C,Nsdictionary,Nspredicate,我使用的是一个NSDictionary,它本身包含一些键及其值的字典 { "1" = { "key1" = "ss", "key2" = "rr", "name" = "nm" }, "2" = { "key1" = "tt", "key2" = "vv", "name" = "gf" }, "3" = { "key1" = "nm"

我使用的是一个NSDictionary,它本身包含一些键及其值的字典

{

"1" = {
        "key1" = "ss",
          "key2" = "rr",
          "name" = "nm"
     },
"2" = {
           "key1" = "tt",
          "key2" = "vv",
           "name" = "gf"
     },
"3" = {
           "key1" = "nm",
          "key2" = "vv",
           "name" = "gf"
     },
"4" = {
           "key1" = "tt",
          "key2" = "vv",
          "name" = "gf"
     },
}
我需要使用NSPredicate过滤key1应该是“tt”而key2应该是“vv”的情况

NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
                   [NSArray arrayWithObjects:@"a", @"b", @"c", nil], @"a",
                   [NSArray arrayWithObjects:@"b", @"c", @"a", nil], @"b",
                   [NSArray arrayWithObjects:@"c", @"a", @"b", nil], @"c",
                   [NSArray arrayWithObjects:@"a", @"b", @"c", nil], @"d",
                   nil];
NSPredicate *p = [NSPredicate predicateWithFormat:@"%@[SELF][0] == 'a'", d];
NSLog(@"%@", p);
NSArray *keys = [d allKeys];
NSArray *filteredKeys = [keys filteredArrayUsingPredicate:p];
NSLog(@"%@", filteredKeys);
NSDictionary *matchingDictionary = [d dictionaryWithValuesForKeys:filteredKeys];
NSLog(@"%@", matchingDictionary);

试试这对你真的很有帮助。

这很有效。您可以在此处设置自己的值

NSArray *data = [NSArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"ss",@"how", nil] forKeys:[NSArray arrayWithObjects:@"key1",@"key1", nil]],[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"tt",@"vv", nil] forKeys:[NSArray arrayWithObjects:@"key1",@"key2", nil]],[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"vv",@"tt", nil] forKeys:[NSArray arrayWithObjects:@"key1",@"key2", nil]],nil];    
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key1 == %@) AND (key2==%@)", @"tt",@"vv"]];
NSLog(@"%@",filtered);
输出:

(
    {
        key1 = tt;
        key2 = vv;
    }
)
(
    {
        key1 = tt;
        key2 = vv;
    }
)
为了更清楚地解释:

NSMutableDictionary *dict4=[[NSMutableDictionary alloc]init];
[dict4 setObject:@"ss" forKey:@"key1"];
[dict4 setObject:@"how" forKey:@"key2"];
NSMutableDictionary *dict5=[[NSMutableDictionary alloc]init];
[dict5 setObject:@"tt" forKey:@"key1"];
[dict5 setObject:@"vv" forKey:@"key2"];
NSMutableDictionary *dict6=[[NSMutableDictionary alloc]init];
[dict6 setObject:@"vv" forKey:@"key1"];
[dict6 setObject:@"tt" forKey:@"key2"];

NSMutableArray  *data = [[NSMutableArray alloc]init];  
[data addObject:dict4];
[data addObject:dict5];
[data addObject:dict6];

NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key1 == %@) AND (key2==%@)", @"tt",@"vv"]];
NSLog(@"%@",filtered);
输出:

(
    {
        key1 = tt;
        key2 = vv;
    }
)
(
    {
        key1 = tt;
        key2 = vv;
    }
)
假定

mainDict = {

"1" = {
        "key1" = "ss",
          "key2" = "rr",
          "name" = "nm"
     },
"2" = {
           "key1" = "tt",
          "key2" = "vv",
           "name" = "gf"
     },
"3" = {
           "key1" = "nm",
          "key2" = "vv",
           "name" = "gf"
     },
"4" = {
           "key1" = "tt",
          "key2" = "vv",
          "name" = "gf"
     },
}
现在,您可以通过以下方式进行过滤:

NSArray *resultArray = [[mainDict allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key1 == %@) AND (key2==%@)", @"tt",@"vv"]];
请尝试以下检查:

NSMutableDictionary *mainDict=[[NSMutableDictionary alloc] init];
for(int i=1; i<=3; i++)
{
    [mainDict setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"tt",@"key1",@"vv",@"key2",@"ttqwdwd",@"name", nil] forKey:[NSString stringWithFormat:@"%i",i]];
}
[mainDict setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"tt",@"key1",@"kk",@"key2",@"ttwwdwd",@"name", nil] forKey:[NSString stringWithFormat:@"%i",4]];
[mainDict setObject:[NSDictionary dictionaryWithObjectsAndKeys:@"tt",@"key1",@"kk",@"key2",@"ttwwdwd",@"name", nil] forKey:[NSString stringWithFormat:@"%i",5]];
NSArray *resultArray = [[mainDict allValues] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(key1 == %@) AND (key2==%@)", @"tt",@"vv"]];
NSLog(@"%@",resultArray);
NSMutableDictionary*mainDict=[[NSMutableDictionary alloc]init];

对于(int i=1;i您可以使用
输入传递测试键
方法
NSDictionary

例如

参考资料:

在此处检查答案: