gpt4 book ai didi

ios - 试图做一个复合谓词。不工作。

转载 作者:行者123 更新时间:2023-12-01 18:02:50 25 4
gpt4 key购买 nike

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

最佳答案

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];

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

关于ios - 试图做一个复合谓词。不工作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6389829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com