尝试创建一个复合谓词。不工作。

发布于 2024-11-16 00:11:16 字数 1209 浏览 3 评论 0原文

我正在尝试为我的核心数据搜索创建一个复合谓词。因此,当用户在搜索栏中输入文本时,它将显示名称、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-11-23 00:11:16
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) || (optionOne contains[cd] %@) || (optionTwo contains[cd] %@)", self.sBar.text];

由于字符串中有 3 个 %@ 标记,因此末尾需要有 3 个 self.sBar.text 表达式。

或者,您可以这样做:

NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];

如果您经常构建此谓词,这会更方便,因为您可以将“模板”谓词存储在 ivar 中。解析谓词并不是最快速的事情,使用模板版本意味着您只需解析一次(而不是每次搜索栏的文本更改时)。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name contains[cd] %@) || (optionOne contains[cd] %@) || (optionTwo contains[cd] %@)", self.sBar.text];

Since you have 3 %@ tokens in your string, you need to have 3 self.sBar.text expressions at the end.

Alternatively, you could do something like this:

NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文