- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 iOS 应用程序中,我的获取 Controller 有一个非常简单的谓词。
NSString *format = [NSString stringWithFormat:@"name like[c] '%@'", nameVar];
NSPredicate *predicate = [NSPredicate predicateWithFormat:format];
[fetchController setPredicate:predicate];
它执行基本的不区分大小写的名称查找。现在我想更改它,以便我可以在搜索框中放入多个单词(nameVar 具有搜索框中的值),并用空格分隔,并让谓词过滤与所有这些关键字匹配的结果。
所以如果我有两个名字:“John Smith”和“Mary Smith”并且我搜索:“Smith M”我只想得到一个结果,但像这样的搜索:“Sm th ith”应该返回两个值。
有人知道应该如何实现吗?
最佳答案
在普通计算机上编辑...
因此有几件事需要注意:
NSExpression
和 NSPredicate
的抽象语法树(特别是 NSComparisonPredicate
和 NSCompoundPredicate
) 对象。您的字符串将被放入 NSConstantValueExpressionType
类型的 NSExpression
中,这意味着它已经将被解释为常规字符串。事实上,将单引号放在格式字符串中将使您的谓词不起作用。nameVar
) 中的“单词”一样多的比较。在这种情况下,我们会将 nameVar
分解为其组成词,并为每个词创建比较。完成此操作后,我们将它们AND
组合在一起以创建一个总体谓词。下面的代码正是这样做的。原始答案
您可以通过构建自己的 NSCompoundPredicate
来做到这一点:
NSString *nameVar = ...; //ex: smith m
NSArray *names = ...; //ex: John Smith, Mary Smith
NSArray *terms = [nameVar componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSMutableArray *subpredicates = [NSMutableArray array];
for(NSString *term in terms) {
if([term length] == 0) { continue; }
NSPredicate *p = [NSPredicate predicateWithFormat:@"name contains[cd] %@", term];
[subpredicates addObject:p];
}
NSPredicate *filter = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
[fetchController setPredicate:filter];
警告:在我的 iPhone 上的浏览器中输入。
关于NSPredicate 用于多词搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4993717/
我有一组 ID:[INT]。现在我想选择所有具有这些 id 的对象,但需要遵循 ID 数组中的 id 顺序。 let IDs = [2, 6, 3, 4, 10, 9] let predicate =
假设我有一个用于 NSPredicate 规则的 Core Data 数据库。 enum PredicateType,Int { case beginswith case endswith case
根据 Apples 类引用 CKQuery,运算符 CONTAINS 是受支持的运算符之一。但是,这似乎不起作用。我有一个名为 myRecord 的 RecordType,以及一个字段名称为 name
这是一个非常基本的问题,即使我认为理解了 NSPredicate 的基础知识,我仍然对为什么我在这里遇到错误感到困惑(searchText 是一个指向正在处理的 NSString 对象的指针传递到方法
在我的 iOS 应用程序中,我的获取 Controller 有一个非常简单的谓词。 NSString *format = [NSString stringWithFormat:@"name like[
我知道您不能将 coredata 视为关系数据库,因为它是一个对象图(如果我错了请纠正我)。因此,当您使用谓词调用提取请求时,我对内存中发生的事情有点迷茫。 是先将整个实体加载到 ManageObje
这必须是重复的。但是有这么多 NSPredicate 问题,我找不到合适的问题。 我有一个包含 compositeName 的 Core Data 对象列表 field 。像“艾米莉布朗特”这样的名字
在我的模型中,我有日期属性并设置它 [NSDate date] ,但得到像这样的谓词 NSPredicate *predicate = [NSPredicate predicateWithForma
我知道如何在关系中使用 NSPredicate,但由于某种原因,当我对获取的属性执行相同的操作时,我得到: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因
首先,感谢您的阅读。 我正在尝试为以下场景构建 NSPredicate。 对象 A 可以有 0 个、1 个或多个对象 B。对象 B 与对象 B 具有逆关系。 我正在尝试对不在 A 中的所有 B 执行获
这个问题基于与我之前的问题相同的应用程序/来源,可以在此处找到: How to manage memory using classes in Objective-C? 我有一个嵌套数组,打印出来时看起
我有以下代码: NSString *mapIDx = @"98"; NSLog(@"map id: %@", mapIDx); NSFetchRequest *request
核心数据NSPredicate中LIKE [c]%@和= [c]%@之间的确切区别是什么?我想搜索一个与接收者完全匹配的字符串。示例: NSArray *arrayNames = [context f
这有点复杂 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate >= %@ AND endDate %@",
我有这个谓词 NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"(date == %@) && (enabled ==
在核心数据中,我有一个名为关键字的实体,其属性为“文本”。我想找到特定字符串中包含的所有关键字对象。 我尝试过: [NSPredicate predicateWithFormat:@"%@ conta
我需要从多个值数组形成一个谓词。所以我想我可以最初形成一个包含所有值的字符串,然后传递该字符串以在谓词中使用,即 NSString* stringForPredicate = [[NSString
如何使用NSPredicate满足多个条件 NSEntityDescription *entity = [NSEntityDescription entityForName:@"MessageData
这是我的问题。我有这个模型,一个[事件]有多个[日],并且[事件]中有一个称为天的关系,并且[日]中有一个反向关系事件。 现在我想传递 Event 对象并使用 NSFetchedResultsCont
一般我们这样使用 NSPredicate : NSPredicate *matchName = [NSPredicate predicateWithFormat:
我是一名优秀的程序员,十分优秀!