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

发布于 2024-10-13 10:05:59 字数 192 浏览 5 评论 0原文

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

我尝试过:

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

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

In core data I have an entity called keyword with a property 'text'. I want to find all keyword objects that are contained in a particular sting.

I tried:

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

But that crashes. It seems like if it will only work if it's reversed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2024-10-20 10:05:59

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

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

The predicate works on the entity, so you have your predicate reversed. Since your entity's text property contains a single word, you'd split the string into multiple words and then test:

// 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];
情话难免假 2024-10-20 10:05:59

我认为你正在做相反的方向。

试试这个。

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

I think you're doing in reverse direction.

Try this.

[NSPredicate predicateWithFormat:@"text contains[cd] %@", myString];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文