问题描述
我正在尝试使用 UISearchDisplayController
和 NSCompoundPredicate
过滤 UITableView's
数据.我有一个带有 3 个 UILabels
的自定义单元格,我希望在搜索中全部过滤,因此是 NSCompoundPredicate
.
I'm trying to filter a UITableView's
data using a UISearchDisplayController
and NSCompoundPredicate
. I have a custom cell with 3 UILabels
that I want to all be filtered within the search, hence the 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];
但是,当我这样做时,编译器会警告我:
However, when I do this, the compiler warns me:
不兼容的指针类型初始化 'NSCompoundPredicate *_strong'带有'NSPredicate *'类型的表达式
我在网上看到的每个例子都做同样的事情,所以我很困惑.NSCompoundPredicate orPredicateWithSubpredicates:
方法在最后一个参数中使用了 (NSArray *)
,所以我真的很困惑.
Every example I've seen online does this exact same thing, so I'm confused. The NSCompoundPredicate orPredicateWithSubpredicates:
method takes an (NSArray *)
in the last parameter, so I'm REALLY confused.
怎么了?
推荐答案
首先,使用contains"很慢,考虑一下beginswith"?其次,你想要的是:
First of all, using "contains" is very slow, consider mayber "beginswith"?Second, what you want is:
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
三,你可以这样做:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.productName beginswith[cd] %@ OR SELF.productManufacturer contains[cd] %@", searchText, searchText];
这篇关于NSCompoundPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!