NSCompoundPredicate

hed*_*ick 6 objective-c uisearchbar uisearchdisplaycontroller nscompoundpredicate

我正在尝试UITableView's使用UISearchDisplayController和过滤数据NSCompoundPredicate.我有一个3的自定义单元格UILabels,我想在搜索中过滤所有内容,因此NSCompoundPredicate.

  // Filter the array using NSPredicate(s)

  NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"SELF.productName contains[c] %@", searchText];
  NSPredicate *predicateManufacturer = [NSPredicate predicateWithFormat:@"SELF.productManufacturer contains[c] %@", searchText];
  NSPredicate *predicateNumber = [NSPredicate predicateWithFormat:@"SELF.numberOfDocuments contains[c] %@",searchText];

  // Add the predicates to the NSArray

  NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicateName, predicateManufacturer, predicateNumber, nil];

  NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,编译器警告我:

不兼容的指针类型使用"NSPredicate*"类型的表达式初始化"NSCompoundPredicate*_strong"

我在网上看到的每个例子都是完全相同的,所以我很困惑.该NSCompoundPredicate orPredicateWithSubpredicates:方法采用(NSArray *)最后一个参数,所以我真的很困惑.

怎么了?

J S*_*iro 13

orPredicateWithSubpredicates:被定义为返回NSPredicate*.您应该能够将最后一行代码更改为:

NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
Run Code Online (Sandbox Code Playgroud)

......并且仍然应用了所有复合预测.


mdo*_*ans 12

首先,使用"包含"非常慢,考虑到mayber"从头开始"?其次,你想要的是:

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
Run Code Online (Sandbox Code Playgroud)

三,你可能只是做了类似的事情:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.productName beginswith[cd] %@ OR SELF.productManufacturer contains[cd] %@", searchText, searchText];
Run Code Online (Sandbox Code Playgroud)