以下是我的谓词:
1) category.enabled == 1 AND (author CONTAINS[c] "John" OR city CONTAINS[c] "John" OR state CONTAINS[c] "John")
2) category.enabled == 1 AND (author CONTAINS[c] "dallas" OR city CONTAINS[c] "dallas" OR state CONTAINS[c] "dallas")
3) category.enabled == 1 AND ((author CONTAINS[c] "John" OR city CONTAINS[c] "John" OR state CONTAINS[c] "John") OR (author CONTAINS[c] "dallas" OR city CONTAINS[c] "dallas" OR state CONTAINS[c] "dallas"))
以下是代码的构造方式:
-(void)findString:(NSString *)string {
if (!string) {
string = @"";
}
NSMutableArray *predicatesArray = [NSMutableArray array];
NSArray *searchQueriesArray = [string componentsSeparatedByString:@" "];
NSPredicate *categoryPredicate = [NSPredicate predicateWithFormat:@"category.enabled == %@",[NSNumber numberWithBool:YES]];
for(NSString *query in searchQueriesArray){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"author CONTAINS[cd] %@ OR city CONTAINS[cd] %@ OR state CONTAINS[cd] %@", query, query, query];
[predicatesArray addObject:predicate];
}
NSPredicate *fieldsSearchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicatesArray];
NSArray *searchPredicatesArray = [NSArray arrayWithObjects:categoryPredicate,fieldsSearchPredicate, nil];
NSPredicate *searchPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:searchPredicatesArray];
self.tracks = [Track findAllSortedBy:@"name" ascending:YES withPredicate:searchPredicate];
[self.tableView reloadData];
}
1)谓词是用string = "John";
构造的
2)with string = "dallas";
3)与string = "John dallas"
问题是:用1)搜索或用2)谓词返回结果,但用3)不返回任何结果。