NSPredicate 连接属性

发布于 2024-12-04 00:55:48 字数 210 浏览 1 评论 0原文

我正在尝试弄清楚如何连接属性名称。我有一个县和一个地区属性,我想要查询,就像

[NSPredicate predicateWithFormat:@"county + district contains[cd] %@",searchBar.text]

给我未实现的 SQL 生成谓词错误一样。我不知道如何实现 NSPredicate。 谢谢

I m trying to figure out how to concatenate attribute names. I have a county and a district attribute that I want to query like

[NSPredicate predicateWithFormat:@"county + district contains[cd] %@",searchBar.text]

gives me unimplemented SQL generation for predicate error. and I am not sure how to implement NSPredicate.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

玩世 2024-12-11 00:55:48

这应该会让您了解如何进行一些更复杂的搜索。它将匹配“县区”、“县区”等查询。

    NSArray *searchTerms = [searchBar.text componentsSeparatedByString:@" "];
    NSString *predicateFormat = @"(county contains[cd] %@) OR (district contains[cd] %@)";
    NSPredicate *predicate;
    if ([searchTerms count] == 1) {
        NSString *term = [searchTerms objectAtIndex:0];
        predicate = [NSPredicate predicateWithFormat:predicateFormat, term, term];
    } else {
        NSMutableArray *subPredicates = [NSMutableArray array];
        for (NSString *term in searchTerms) {
            NSPredicate *p = [NSPredicate predicateWithFormat:predicateFormat, term, term];
            [subPredicates addObject:p];
        }
        predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
    }
    [fetchRequest setPredicate:predicate];

请参阅 Cocoa 是我的女朋友:向您的 Core Data 应用程序添加 iTunes 式搜索以获取更多说明。

This should give you an idea of how to do some more complicated searching. It will match queries for "county district", "district county", etc.

    NSArray *searchTerms = [searchBar.text componentsSeparatedByString:@" "];
    NSString *predicateFormat = @"(county contains[cd] %@) OR (district contains[cd] %@)";
    NSPredicate *predicate;
    if ([searchTerms count] == 1) {
        NSString *term = [searchTerms objectAtIndex:0];
        predicate = [NSPredicate predicateWithFormat:predicateFormat, term, term];
    } else {
        NSMutableArray *subPredicates = [NSMutableArray array];
        for (NSString *term in searchTerms) {
            NSPredicate *p = [NSPredicate predicateWithFormat:predicateFormat, term, term];
            [subPredicates addObject:p];
        }
        predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
    }
    [fetchRequest setPredicate:predicate];

See Cocoa Is My Girlfriend: Adding iTunes-style search to your Core Data application for more explanation.

要走干脆点 2024-12-11 00:55:48

我最终实现了另一个字段作为连接两个变量(地区+国家)并对该变量执行查询。

I ended up implementing another field as concatenating the two variables(district+country) and perform the query on that variable.

倾其所爱 2024-12-11 00:55:48

我做了类似的事情并得出结论,像 cekisakurek 一样,最好的方法是将字段连接到一个公共字段(与名字/姓氏更相关)

- (NSString *)fullName {
    return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}

,然后使用“contains[cd]”过滤该字段

[NSPredicate predicateWithFormat:@"fullName contains[cd] %@", self.searchBar.text];

I did something similar and concluded that like cekisakurek, the best method was to concatenate the fields into a common field (more relevant for first/last name)

- (NSString *)fullName {
    return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
}

and then filtered on this field using 'contains[cd]'

[NSPredicate predicateWithFormat:@"fullName contains[cd] %@", self.searchBar.text];
等风也等你 2024-12-11 00:55:48
[NSPredicate predicateWithFormat:@"county contains[cd] %@ AND district contains[cd] %@",searchBar.text,searchBar.text];

只需尝试上面的代码行,它会对您有所帮助。

[NSPredicate predicateWithFormat:@"county contains[cd] %@ AND district contains[cd] %@",searchBar.text,searchBar.text];

Just try the above lines of code, it would helps you.

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