如何在NsmutableArray中过滤多个字符串

时间:2015-11-24 12:48:16

标签: objective-c nsmutablearray nspredicate nsmutabledictionary

大家好,我的项目中我有NSMutableArray包含Number of Dictionary值,这里我用一个字符串过滤,但现在我想过滤这个数组中的多个字符串,下面是我的数组值

(
        {
        BusAC = "A/C";
        BusFare = 550;
        BusType = "Semi Sleeper";
        TravelsName = "ABC Travels";
    },
        {
        BusAC = "Non A/C";
        BusFare = 600;
        BusType = Seater;
        TravelsName = "Arulmigu Aandal Azahagar Madurai Express";
    },
        {
        BusAC = "A/C";
        BusFare = 1250;
        BusType = Sleeper;
        TravelsName = "GSP  Travels";
    },
        {
        BusAC = "Non A/C";
        BusFare = 350;
        BusType = "Semi Sleeper";
        TravelsName = "Madura  Travels";
    },
        {
        BusAC = "A/C";
        BusFare = 420;
        BusType = Seater;
        TravelsName = "MJT Travels";
    },
        {
        BusAC = "A/C";
        BusFare = 1500;
        BusType = Sleeper;
        TravelsName = "OPR Travels";
    },
        {
        BusAC = "Non A/C";
        BusFare = 730;
        BusType = Seater;
        TravelsName = "MJT Travels";
    }
)

在这个过滤了一个字符串,如Sleeper bus

NSPredicate *predicateStr1 = [NSPredicate predicateWithFormat:@"%K == %@", @"BusType",@"Sleeper"];
 NSMutableArray *filteredArray1 = [NSMutableArray arrayWithArray:[totDictArray filteredArrayUsingPredicate:predicateStr1]];

然后我用两个字符串过滤,如Sleeper& AC

NSPredicate * prediStr=[NSPredicate predicateWithFormat:@"BusType == %@ AND BusAC == %@",@"Sleeper",@"A/C"];

这也很好但现在我想过滤像阵列&串

String is A/C & Array value is (
    "ABC Travels",
    "GSP  Travels"
) 

我试过这段代码 这里的arr是上面提到的数组

 for (int i=0; i<arr.count; i++)
        {
            NSPredicate * prediStr=[NSPredicate predicateWithFormat:@"TravelsName == %@ AND BusAC == %@",arr[i],@"A/C"];
            NSLog(@"prediStr %@",prediStr);
            NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[totDictArray filteredArrayUsingPredicate:prediStr]];
                NSLog(@"filteredArray %@",filteredArray);
        }

帮帮我......

已更新

(
        {
        Boarding =         (
            "CMBT Omini BS",
            CMBT,
            Vadapalani,
            "Ashok Nagar",
            Thambaram,
            Perungulathur,
            Vandalur
        );
        BusAC = "A/C";
        BusFare = 550;
        BusType = "Semi Sleeper";
        Dropping = "Trichy Bypass";
        TravelsName = "ABC Travels";
    },
        {
        Boarding =         (
            "CMBT Omini BS",
            CMBT,
            Vadapalani,
            "Ashok Nagar",
            Thambaram,
            Perungulathur,
            Vandalur
        );
        BusAC = "Non A/C";
        BusFare = 600;
        BusType = Seater;
        Dropping = "Chathram BS";
        TravelsName = "Arulmigu Aandal Azahagar Madurai Express";
    },
        {
        Boarding =         (
            "CMBT Omini BS",
            CMBT,
            Vadapalani,
            "Ashok Nagar",
            Thambaram,
            Perungulathur,
            Vandalur
        );
        BusAC = "A/C";
        BusFare = 1250;
        BusType = Sleeper;
        Dropping = "Thillai Nagar";
        TravelsName = "GSP  Travels";
    },
        {
        Boarding =         (            
            Thambaram,
            Perungulathur,
            Vandalur
        );
        BusAC = "Non A/C";
        BusFare = 350;
        BusType = "Semi Sleeper";
        Dropping = "Trichy Bypass";
        TravelsName = "Madura  Travels";
    },
        {
        Boarding =         (
            "CMBT Omini BS",
            CMBT,
            Vadapalani,
            "Ashok Nagar",
            Guindy,
            Velachery,
            Thambaram,
        );
        BusAC = "A/C";
        BusFare = 420;
        BusType = Seater;
        Dropping = "Court Campus";
        TravelsName = "MJT Travels";
    },
        {
        Boarding =         (
            "CMBT Omini BS",
            CMBT,
            Thambaram,
            Perungulathur,
            Vandalur
        );
        BusAC = "A/C";
        BusFare = 1500;
        BusType = Sleeper;
        Dropping = "Central BS";
        TravelsName = "OPR Travels";
    },
        {
        Boarding =         (
            "CMBT Omini BS",
            CMBT,
            Vadapalani,
            "Ashok Nagar",
            Thambaram,
        );
        BusAC = "Non A/C";
        BusFare = 730;
        BusType = Seater;
        Dropping = "Omini BS";
        TravelsName = "MJT Travels";
    }
)

在这个阵列中我也登上了这个我想在登机阵列上过滤也意味着BusAC是A / c&amp;登机是

 (Guindy,
            Velachery,
            Thambaram,)

1 个答案:

答案 0 :(得分:1)

要确定是否在数组中找到值,请使用IN运算符。

[NSPredicate predicateWithFormat:@"TravelsName IN %@", array];