过滤具有两个字符串的词典数组

时间:2016-06-04 12:33:42

标签: ios objective-c json nspredicate

对于搜索下拉列表,我需要分别在单元格textlabel和详细的Textlabel中显示“Description”和“SearchTerms”。我需要实现基于两个字符串的过滤器,即搜索文本应该与“描述”和“搜索字符串”进行比较。我尝试过NSPredicate但它只能过滤一个字符串

NSString *substring = [NSString stringWithString:searchServiceTextField.text];
NSLog(@"substring %@",substring);
NSMutableArray *arr2Filt= [searchArray valueForKey:@"Description"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF  contains[c] %@",substring];
NSArray *filteredarr = [NSMutableArray arrayWithArray:[arr2Filt filteredArrayUsingPredicate:predicate]];
NSLog(@"filtered array %@",filteredarr);

我试图修改两个字符串,但我没有得到更好的灵魂。

  [{
        Description = "Rental Jewellery, Jewellery for events";
        ProfessionId = "<null>";
        SKUFormat = "<null>";
        SearchTerms = "Rental Jewellery";
        SpecialityId = 62;
        tokens = "<null>";
        value = "<null>";
    },
    {
        Description = "Kids Party with Food and Games Venues";
        ProfessionId = "<null>";
        SKUFormat = "<null>";
        SearchTerms = "Party Venue";
        SpecialityId = 63;
        tokens = "<null>";
        value = "<null>";
    },
    {
        Description = "Music, Dance, Sports classes";
        ProfessionId = "<null>";
        SKUFormat = "<null>";
        SearchTerms = Classes;
        SpecialityId = 64;
        tokens = "<null>";
        value = "<null>";
    },
    {
        Description = "Zumba, Yoga, Aerobics classes";
        ProfessionId = "<null>";
        SKUFormat = "<null>";
        SearchTerms = "Fitness/Health";
        SpecialityId = 65;
        tokens = "<null>";
        value = "<null>";
    }
    ]

1 个答案:

答案 0 :(得分:1)

你可以这样做

NSString *substring = [NSString stringWithString:searchServiceTextField.text];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Description contains[c] %@ AND SearchTerms contains[c] %@",substring, substring];
NSArray * filteredarr =[[searchArray filteredArrayUsingPredicate:predicate] copy];

NSLog(@"filtered array %@",filteredarr);