尝试创建一个复合谓词。不工作。
我正在尝试为我的核心数据搜索创建一个复合谓词。因此,当用户在搜索栏中输入文本时,它将显示名称、optionOne 或 optionTwo 属性中包含该文本的任何内容的结果。
我尝试过这个:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text !=nil) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) || (optionOne contains[cd] %@) || (optionTwo contains[cd] %@)", self.sBar.text];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[self.myTable reloadData];
[sBar resignFirstResponder];
}
但它只是在没有描述性原因的情况下崩溃了。所以我认为我需要采用这三个谓词并以某种方式将它们组合起来:
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", self.sBar.text];
NSPredicate *optionOnePredicate = [NSPredicate predicateWithFormat:@"optionOne contains[cd] %@", self.sBar.text];
NSPredicate *optionTwoPredicate = [NSPredicate predicateWithFormat:@"optionTwo contains[cd] %@", self.sBar.text];
I am trying to make a compound predicate for my core data search. So when the user enters text in the search bar, it will display results for anything that has that text in either the name, optionOne or optionTwo attributes.
I tried this:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text !=nil) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) || (optionOne contains[cd] %@) || (optionTwo contains[cd] %@)", self.sBar.text];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
[self.myTable reloadData];
[sBar resignFirstResponder];
}
But it just crashes without a descriptive reason. So I think I need to take these three predicates and somehow combine them:
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", self.sBar.text];
NSPredicate *optionOnePredicate = [NSPredicate predicateWithFormat:@"optionOne contains[cd] %@", self.sBar.text];
NSPredicate *optionTwoPredicate = [NSPredicate predicateWithFormat:@"optionTwo contains[cd] %@", self.sBar.text];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于字符串中有 3 个
%@
标记,因此末尾需要有 3 个self.sBar.text
表达式。或者,您可以这样做:
如果您经常构建此谓词,这会更方便,因为您可以将“模板”谓词存储在 ivar 中。解析谓词并不是最快速的事情,使用模板版本意味着您只需解析一次(而不是每次搜索栏的文本更改时)。
Since you have 3
%@
tokens in your string, you need to have 3self.sBar.text
expressions at the end.Alternatively, you could do something like this:
This is handier if you're building this predicate a lot, because you can store the "template" predicate in an ivar. Parsing a predicate is not the snappiest of things, and using the template version means you'll only have to parse it once (instead of every time the search bar's text changes).