web-dev-qa-db-ja.com

述語によるNSDictionaryのフィルタリング

私はそれ自体が辞書にいくつかのキーとその値を含む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」にする場合でフィルタリングする必要があります。

15
Nassif

と仮定する

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);
25
Simha.IC

これは機能します。ここで独自の値を設定できます。

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

より明確な説明のために:

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;
    }
)
6
Balu
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);

これは本当に役に立ちます。

2
Jitendra

keysOfEntriesPassingTestNSDictionaryメソッドを使用できます。

例えば。

NSSet *keys = [dict keysOfEntriesPassingTest:^BOOL(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
NSDictionary *object =(obj); 
return ([@"tt" isEqualToString:object[@"key1] &&[@"vv"isEqualToString:object[@"key2"]);
}];

参照: Apple Doc

1
Min Soe