在核心数据中,我有一个名为关键字的实体,其属性为“文本”。我想找到特定字符串中包含的所有关键字对象。
我尝试过:
[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/