OStack程序员社区-中国程序员成长平台

标题: ios - 用两个字符串过滤字典数组 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 16:57
标题: ios - 用两个字符串过滤字典数组

对于搜索下拉表,我需要分别在单元格文本标签和详细文本标签中显示“描述”和“搜索词”。我需要实现基于两个字符串的过滤器,即搜索文本应该与“描述”和“搜索词”进行比较。我试过 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 = "arty 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>";
    }
    ]



Best Answer-推荐答案


你可以这样做

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

关于ios - 用两个字符串过滤字典数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630285/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4