iphone - NSPredicate 用于获取字符串中的关键字

标签 iphone core-data nspredicate

在核心数据中,我有一个名为关键字的实体,其属性为“文本”。我想找到特定字符串中包含的所有关键字对象。

我尝试过:

[NSPredicate predicateWithFormat:@"%@ contains[c] text", myString];

但是那会崩溃。好像只有反过来才有效。

最佳答案

谓词适用于实体,因此您的谓词被颠倒了。由于实体的文本属性包含单个单词,因此您需要将该字符串拆分为多个单词,然后进行测试:

// This is very simple and only meant to illustrate the property, you should
// use a proper regex to divide your string and you should probably then fold
// and denormalize the components, etc.
NSSet* wordsInString = [NSSet setWithArray:[myString componentsSeparatedByString:@" "]];
NSPredicate* pred = [NSPredicate predicateWithFormat:@"SELF.text IN %@", wordsInString];

关于iphone - NSPredicate 用于获取字符串中的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4744693/

相关文章:

iphone - 第一次使用断点

swift - removePersistentStore(_ :) doesn't return Bool in Swift

iphone - 'NSInvalidArgumentException',原因 : 'Unable to parse the format string "title contains[cd] @ %"' Error

iphone - 为什么状态栏和主视图之间有间隙

javascript - 在 iPhone APP 中集成 Disqus

ios - 使用核心数据从 TableView 打开编辑 View Controller

ios - NSFetchedResultsController 的部分数组元素的名称未正确更新

ios - 使用 NSPredicate IOS 取反值

ios - NSPredicate在两个日期之间NSInvalidArgumentException

ios - 符合 Swift NSObject 子类的 Objective-C 协议(protocol)的问题