如何使用 NSPredicate 通过多文本查询搜索文本?
How to search text with a multi text query using NSPredicate?
我正在尝试通过多个值执行查询。
不幸的是,我不能使用 ANY 运算符,因为我必须执行 CONTAINS 操作。
示例:
Obj *foo1 = [Obj objectWithName:@"John" age:42];
Obj *foo2 = [Obj objectWithName:@"Elizabeth" age:21];
Obj *foo3 = [Obj objectWithName:@"Albert" age:17];
Obj *foo4 = [Obj objectWithName:@"Marty" age:54];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
// 这个查询应该 return foo1
, foo2
和 foo4
.
我尝试了类似的方法,但它不起作用:
[NSPredicate predicateWithFormat:@"(name CONTAINS[cd]) IN %@, query];
有什么建议吗?
NSMutableArray *filterArray = [NSMutableArray array];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
for (NSString *string in query)
{
[filterArray addObjectsFromArray:[mainArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] IN %@", string]]];
}
此处 mainarray 是包含数组的对象。
或者试试这个..
NSMutableArray *filterArray = [NSMutableArray array];
NSMutableArray *predicateArray = [NSMutableArray array];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
for (NSString *string in query)
[predicateArray addObject:[NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", string]];
NSPredicate *compoundpred = [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray];
[filterArray addObjectsFromArray:[mainArray filteredArrayUsingPredicate:compoundpred];
希望对您有所帮助...
我正在尝试通过多个值执行查询。 不幸的是,我不能使用 ANY 运算符,因为我必须执行 CONTAINS 操作。
示例:
Obj *foo1 = [Obj objectWithName:@"John" age:42];
Obj *foo2 = [Obj objectWithName:@"Elizabeth" age:21];
Obj *foo3 = [Obj objectWithName:@"Albert" age:17];
Obj *foo4 = [Obj objectWithName:@"Marty" age:54];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
// 这个查询应该 return foo1
, foo2
和 foo4
.
我尝试了类似的方法,但它不起作用:
[NSPredicate predicateWithFormat:@"(name CONTAINS[cd]) IN %@, query];
有什么建议吗?
NSMutableArray *filterArray = [NSMutableArray array];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
for (NSString *string in query)
{
[filterArray addObjectsFromArray:[mainArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name CONTAINS[cd] IN %@", string]]];
}
此处 mainarray 是包含数组的对象。
或者试试这个..
NSMutableArray *filterArray = [NSMutableArray array];
NSMutableArray *predicateArray = [NSMutableArray array];
NSArray *query = @[@"Joh", @"lizabe", @"arty"];
for (NSString *string in query)
[predicateArray addObject:[NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", string]];
NSPredicate *compoundpred = [NSCompoundPredicate andPredicateWithSubpredicates:predicateArray];
[filterArray addObjectsFromArray:[mainArray filteredArrayUsingPredicate:compoundpred];
希望对您有所帮助...