NSString *predicateFormat 如何搜索两个实体

发布于 2024-09-05 00:19:26 字数 794 浏览 5 评论 0原文

你好,很抱歉问了这个愚蠢的问题,但我想我可能在这里遗漏了一些简单的东西,并且自己无法弄清楚。

我尝试使用以下代码搜索表视图:

- (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 技术交流群。

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

发布评论

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

评论(1

以酷 2024-09-12 00:19:26

而不是:

NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];

您有三个选项:

NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString, searchString];

或:

NSString *predicateFormat = @"(name contains[c] %1$@) OR (age contains[c] %1$@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];

或:

NSString *predicateFormat = @"(name contains[c] $SEARCH) OR (age contains[c] $SEARCH)";
NSPredicate *template = [NSPredicate predicateWithFormat:predicateFormat];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:[NSDictionary dictionaryWithObject:searchString forKey:@"SEARCH"]];

如果您正在构建具有未知数量替换的大型谓词,则第三个选项很不错。就不重复自己而言,第二个可能是最简单的,但第一个是最容易理解的。

Instead of:

NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];

You have three options:

NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString, searchString];

Or:

NSString *predicateFormat = @"(name contains[c] %1$@) OR (age contains[c] %1$@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];

Or:

NSString *predicateFormat = @"(name contains[c] $SEARCH) OR (age contains[c] $SEARCH)";
NSPredicate *template = [NSPredicate predicateWithFormat:predicateFormat];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:[NSDictionary dictionaryWithObject:searchString forKey:@"SEARCH"]];

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.

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