Objective c 复合谓词
我正在尝试使用Objective c 复合谓词,objective-c,uisearchbar,uisearchdisplaycontroller,nscompoundpredicate,Objective C,Uisearchbar,Uisearchdisplaycontroller,Nscompoundpredicate,我正在尝试使用UISearchDisplayController和NSCompoundPredicate过滤UITableView的数据。我有一个带有3个UILabels的自定义单元格,我希望在搜索中对其进行过滤,因此使用了NSCompoundPredicate // Filter the array using NSPredicate(s) NSPredicate *predicateName = [NSPredicate predicateWithFormat:@"SELF.pro
UISearchDisplayController
和NSCompoundPredicate
过滤UITableView的
数据。我有一个带有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];
但是,当我执行此操作时,编译器会警告我:
初始化“NSCompoundPredicate*\u strong”的指针类型不兼容
使用“NSPredicate*”类型的表达式
我在网上看到的每一个例子都做了同样的事情,所以我很困惑。NSCompoundPredicate或predicatewithsubpredicates:
方法在最后一个参数中使用了一个(NSArray*)
,所以我真的很困惑
怎么了?
或带有子谓词的预测:
被定义为返回NSPredicate*。您应该能够将最后一行代码更改为:
NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
。。。还有所有的复合谓语应用。 < P>首先,使用“包含”是非常慢的,考虑MayBER“初学者”吗?
第二,你想要的是:
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
第三,你可以这样做:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.productName beginswith[cd] %@ OR SELF.productManufacturer contains[cd] %@", searchText, searchText];
下面是我根据上面的答案创建的一个有用的方法(我非常感谢!) 它允许通过发送筛选器项数组和表示搜索条件的字符串,动态创建NSPredicate 在原始情况下,搜索条件会更改,因此它应该是数组而不是字符串。但无论如何,这可能会有所帮助
- (NSPredicate *)dynamicPredicate:(NSArray *)array withSearchCriteria:(NSString *)searchCriteria
{
NSArray *subPredicates = [[NSArray alloc] init];
NSMutableArray *subPredicatesAux = [[NSMutableArray alloc] init];
NSPredicate *predicate;
for( int i=0; i<array.count; i++ )
{
predicate = [NSPredicate predicateWithFormat:searchCriteria, array[i]];
[subPredicatesAux addObject:predicate];
}
subPredicates = [subPredicatesAux copy];
return [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
}
-(NSPredicate*)动态预测:(NSArray*)带搜索条件的数组:(NSString*)搜索条件
{
NSArray*子预测=[[NSArray alloc]init];
NSMUTABLEARRY*子预测SAUX=[[NSMUTABLEARRY alloc]init];
NSPredicate*谓词;
例如(inti=0;我之前就试过做这件事,但结果是崩溃了——我想这就是连续工作15个小时不睡觉的代价:)