2011-01-20 80 views
4

在核心数据中,我有一个名为keyword的实体具有“text”属性。我想查找包含在特定sting中的所有关键字对象。NSPredicate用于获取字符串中的关键字

我想:

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

但这崩溃。看起来好像它只会在倒过来时才起作用。

+0

你能解释一下你想要的更好吗?你正在寻找所有的关键字实体`k`,其中`k.text包含myString`,或者所有的实体`k.myString包含文本`,其中myString是关键字上的变量属性名? – Tim 2011-01-20 08:06:32

+0

相反,我正在寻找在mystring中包含k.text(包含在内)的所有关键字实体。另外应该注意的是,关键字可能比单个单词长,所以它们更像是关键短语。 – 2011-01-20 23:31:00

回答

3

谓词在实体上起作用,所以你的谓词颠倒了。由于您的实体的文本属性包含一个字,你会分割字符串转换成多个单词,然后测试:

// 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]; 
0

我觉得你在反方向做。

试试这个。

[NSPredicate predicateWithFormat:@"text contains[cd] %@", myString]; 
相关问题