gpt4 book ai didi

objective-c - NSPredicate 连接属性

转载 作者:可可西里 更新时间:2023-11-01 04:21:45 25 4
gpt4 key购买 nike

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

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

为谓词错误提供未实现的 SQL 生成。而且我不确定如何实现 NSPredicate。谢谢

最佳答案

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

    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 Is My Girlfriend: Adding iTunes-style search to your Core Data application以获得更多解释。

关于objective-c - NSPredicate 连接属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7385472/

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