NSString *predicateFormat 如何搜索两个实体
你好,很抱歉问了这个愚蠢的问题,但我想我可能在这里遗漏了一些简单的东西,并且自己无法弄清楚。
我尝试使用以下代码搜索表视图:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];
self.fetchedResultsController = [self resetFetchedResultsController:predicate cached:NO];
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
我想要实现的是,当用户搜索表时,不仅可以按“姓名”搜索,还可以按“年龄”搜索!
我上面的代码只搜索“名称”
我错过了一些简单的东西吗?
谢谢您的宝贵时间
hello there and sorry for the stupid question but i think i might be missing something simple here and can t figure it out myself.
i m trying to search a table view using the following code:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];
self.fetchedResultsController = [self resetFetchedResultsController:predicate cached:NO];
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
what i want to achieve is when the user searches the table to be able to search not only by "name" but with "age" as well!
my code above only searches the "name"
Am i missing something simple?
thank you for your time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是:
您有三个选项:
或:
或:
如果您正在构建具有未知数量替换的大型谓词,则第三个选项很不错。就不重复自己而言,第二个可能是最简单的,但第一个是最容易理解的。
Instead of:
You have three options:
Or:
Or:
The 3rd option is nice if you're building a large predicate that has an unknown number of substitutions. The 2nd is probably the simplest in terms of not repeating yourself, but the 1st is easiest to understand.