【问题标题】:Multiple Search Parameter in NSPradicateNSPrdicate 中的多个搜索参数
【发布时间】:2012-08-29 09:09:57
【问题描述】:

我想根据多个参数搜索我的 eventList 数组 像标题,结束日期等... 我正在使用以下代码 当我只使用一个参数标题时,如 [cd] %@ 它完美地工作

        NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimmed = [searchBar.text stringByTrimmingCharactersInSet:whitespace];
        if([trimmed isEqualToString:@""]==NO)
        {
            //        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title like[cd] %@",[NSString stringWithFormat:@"*%@*", searchBar.text]];
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title like[cd] %@ ||  enddate like[cd] %@  ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]];

            NSLog(@"Main Array:%@",arrMain);
            eventsList =(NSMutableArray*) [arrMain filteredArrayUsingPredicate:predicate];
            [eventsList retain];
            NSLog(@"Search Result:%@",eventsList);
            [eventTable reloadData];
        }

我试过了
NSPredicate predicate = [NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"%@", searchBar.text],[NSString stringWithFormat:@"%@*", searchBar.text]];

它也有效,但不准确,因为我正在搜索它不会显示的特定日期 但如果我写 2 或 p 或 a 它只适用于某些结果......所以我可以说它不完美 和 我的结束日期是格式为 2012-09-04 18:28:02 的字符串 任何人都可以帮我解决这个问题....

【问题讨论】:

  • 我尝试过使用 NSPredicate predicate = [NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@ "*%@", searchBar.text],[NSString stringWithFormat:@"%@", searchBar.text]];

标签: iphone search nspredicate


【解决方案1】:

完成

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]];

我检查了我的数据库,它包含 2012-09-04 18:28:02 格式的日期,并且我以其他格式显示。

【讨论】:

    【解决方案2】:

    将各个谓词的适当集合组装到 NSCompoundPredicate 中

    NSMutableArray *parr = [NSMutableArray array];
    if([brand_one length]) { 
        [parr addObject:[NSPredicate predicateWithFormat:@"(title like[cd] %@) OR (enddate like[cd] %@) ",[NSString stringWithFormat:@"*%@*", searchBar.text],[NSString stringWithFormat:@"*%@*", searchBar.text]]];
    }
    if([brand_two length]) { 
         // etc 
    }
    
    NSCompoundPredicate *cpred = [NSCompoundPredicate andPredicateWithSubpredicates:parr];
    

    您可以将谓词与 orPredicateWithSubpredicates: 和 andPredicateWithSubpredicates: 叠加,并且 NSCompoundPredicate 作为 NSPredicate 本身可以复合。

    http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSCompoundPredicate_Class/Reference/Reference.html

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      相关资源
      最近更新 更多