Iphone NSPredicate用于获取字符串中的关键字
在核心数据中,我有一个名为keyword的实体,其属性为“text”。我想找到包含在特定sting中的所有关键字对象 我试过:Iphone NSPredicate用于获取字符串中的关键字,iphone,core-data,nspredicate,Iphone,Core Data,Nspredicate,在核心数据中,我有一个名为keyword的实体,其属性为“text”。我想找到包含在特定sting中的所有关键字对象 我试过: [NSPredicate predicateWithFormat:@"%@ contains[c] text", myString]; 但这会崩溃。似乎只有当它反转的时候它才会起作用 谓词作用于实体,因此您可以反转谓词。由于实体的text属性包含一个单词,因此您需要将字符串拆分为多个单词,然后测试: // This is very simple and only me
[NSPredicate predicateWithFormat:@"%@ contains[c] text", myString];
但这会崩溃。似乎只有当它反转的时候它才会起作用 谓词作用于实体,因此您可以反转谓词。由于实体的text属性包含一个单词,因此您需要将字符串拆分为多个单词,然后测试:
// 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];
我认为你在做相反的事情 试试这个
[NSPredicate predicateWithFormat:@"text contains[cd] %@", myString];
你能解释清楚你想要什么吗?您是在查找所有关键字实体
k
,其中k.text包含myString
,还是所有k.myString包含text
的实体,其中myString是关键字的变量属性名称?相反,我在查找myString中包含k.text的所有关键字实体。还需要注意的是,关键字可能比单个单词长,因此它们更像是关键短语。如果我想匹配目标短语中的多个单词,该怎么办?例如,我的keyword.text是“根啤酒”,我希望它与“鹅岛根啤酒”相匹配。我可以拆分表达式的两侧并检查关键字的两个部分是否都在wordsInString集中吗?