Filtering NSDictionary with predicate

前端 未结 4 1749
一生所求
一生所求 2020-12-13 15:31

I am using a NSDictionary that itself contains dictionaries some keys and its values.The format is like ,

{

\"1\" = {
        \"key1\" = \"ss\",
          \         


        
4条回答
  •  清歌不尽
    2020-12-13 16:07

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

    try this really helpful to you.

提交回复
热议问题