如何在ios

时间:2015-11-25 12:25:11

标签: objective-c nsmutablearray nspredicate nsmutabledictionary

我项目中的每个人都有一个包含NSDictionary的NSMutable数组

我按键和按键完成了过滤价值

 NSPredicate * prediStr=[NSPredicate predicateWithFormat:@"BusAC == %@",@"AC"];
 filteredArray = [NSMutableArray arrayWithArray:[totDictArray filteredArrayUsingPredicate:prediStr]];

然后我用数组值

过滤了密钥
for (int i=0; i<arr.count; i++)
 {
   NSPredicate * prediStr=[NSPredicate predicateWithFormat:@"TravelsName IN %@",arr[i]];
   filteredArray = [NSMutableArray arrayWithArray:[filteredArray filteredArrayUsingPredicate:prediStr]];                
 }

,这里我的问题是我想在这里过滤包含Dictionary的数组值我的数组值:

(
        {
        Boarding =         (
                        {
                "board_id" = 0;
                "board_value" = "CMBT Omini BS";
            },
                        {
                "board_id" = 1;
                "board_value" = CMBT;
            },
                        {
                "board_id" = 2;
                "board_value" = Thambaram;
            }
        );
        BusAC = "A/C";
        BusFare = 550;
        BusType = "Semi Sleeper";
        TravelsName = "ABC Travels";
    },
        {
        Boarding =         (
                        {
                "board_id" = 0;
                "board_value" = CMBT;
            },
                        {
                "board_id" = 1;
                "board_value" = Guindy;
            },
                        {
                "board_id" = 2;
                "board_value" = Thambaram;
            }
        );
        BusAC = "A/C";
        BusFare = 600;
        BusType = Seater;
        TravelsName = "Arulmigu Aandal Azahagar Madurai Express";
    },
        {
        Boarding =         (
                        {
                "board_id" = 0;
                "board_value" = "CMBT Omini BS";
            },
                        {
                "board_id" = 1;
                "board_value" = CMBT;
            },
                        {
                "board_id" = 2;
                "board_value" = Thambaram;
            }
        );
        BusAC = "A/C";
        BusFare = 1250;
        BusType = Sleeper;
        TravelsName = "GSP  Travels";
    },
        {
        Boarding =         (
                        {
                "board_id" = 0;
                "board_value" = CMBT;
            },
                        {
                "board_id" = 1;
                "board_value" = Guindy;
            },
                        {
                "board_id" = 2;
                "board_value" = Velachery;
            }
        );
        BusAC = "Non A/C";
        BusFare = 350;
        BusType = "Semi Sleeper";
        TravelsName = "Madura  Travels";
    }
)

在此我想要过滤

 Boarding Values
(
    CMBT,
    Guindy

) & 
BusAC Value A/C

帮帮我

1 个答案:

答案 0 :(得分:0)

据我所知,你需要对字典数组进行排序,在每个字典中你需要排序一些内部数组。所以实际上你需要用局部排序的结果修改每个内部数组,然后对这个全局数组进行排序。 事实上,NSPredicate并没有修改元素,因此我建议&#34;解包&#34;这本词典成:

.....
{
    BusAC = "A/C";
    BusFare = 600;
    BusType = Seater;
    TravelsName = "Arulmigu Aandal Azahagar Madurai Express";
    "board_id" = 0;
    "board_value" = CMBT;
},
    {
    BusAC = "A/C";
    BusFare = 600;
    BusType = Seater;
    TravelsName = "Arulmigu Aandal Azahagar Madurai Express";
    "board_id" = 1;
    "board_value" = Guindy;
},
.....

为方便起见,我建议使用{{3}}或任何其他解决方案。

 NSArray *unpackedArray = [testArray linq_selectMany:^id(NSDictionary<NSString *,id> *item) {
    NSMutableDictionary *resultDic = [item mutableCopy];
    [resultDic removeObjectForKey:@"Boarding"];
    NSArray *boarding = item[@"Boarding"];
    return [boarding linq_select:^id(NSDictionary *item) {
        return [item linq_Merge:resultDic];
    }];
}];
NSLog(@"unpacked: %@", unpackedArray );

然后你可以根据需要过滤它,使用谓词或其他东西

unpackedArray = [[unpackedArray linq_where:^BOOL(NSDictionary *item) {
    return [@[@"CMBT", @"Guindy"] containsObject:item[@"board_value"]];
}] linq_where:^BOOL(NSDictionary *item) {
    return [item[@"BusAC"] isEqualToString:@"A/C"];
}];
NSLog(@"unpacked: %@", unpackedArray );