如何创建 NSPredicate 来查找具有前导数值的条目?

发布于 2024-10-08 22:15:22 字数 239 浏览 5 评论 0原文

我正在使用 NSPredicates 根据名称属性获取实体。为以字母开头的名称创建谓词很容易(@“name BEGINSWITH %@”,searchLetter),但是现在我想获取名称以数字值(或更确切地说是非字母字符)开头的所有实体。

这里合适的谓词表达式是什么?

现在我不想太深入谓词编程,因为这就是我现在所需要的,而且时间过得真快。所以,请不要向我指出谓词编程指南,我只需要那个表达式..:)

非常感谢大家!

I'm using NSPredicates to fetch entities based on a name attribute. Creating a predicate for names beginning with letters was easy (@"name BEGINSWITH %@", searchLetter), however now I'd like to fetch all entities with a name that begins with a numerical value, or rather a non-alphabetical character.

What would be the appropriate predicate expression here?

Right now I don't want to get too deep into predicate programming, as this is all I need right now and time flies. So, please, don't point me to the Predicate Programming Guide, I just need that expression.. :)

Thanks alot guys!

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

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

发布评论

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

评论(2

世俗缘 2024-10-15 22:15:22

如果 MATCHES 运算符不适用于获取请求,那么您实际上必须手动执行操作:

NOT(name BEGINSWITH[cd] 'a' OR name BEGINSWITH[cd] 'b' OR ... OR name BEGINSWITH[cd] 'z')

这将获取不以字母开头的所有内容。同样:

name BEGINSWITH[cd] '0' OR name BEGINSWITH[cd] '1' OR ... OR name BEGINSWITH[cd] '9'

将获得以数字开头的所有内容。

编辑评论引发了另一个想法:

如果您可以提取name的第一个字母,您可以这样做:(

NOT(name[0] IN %@), arrayOfLetters

name[0]name 的第一个字符,并且 arrayOfLetters 是字母字符的 NSArray

If the MATCHES operator doesn't work with a fetch request, then you'll essentially have to do stuff manually:

NOT(name BEGINSWITH[cd] 'a' OR name BEGINSWITH[cd] 'b' OR ... OR name BEGINSWITH[cd] 'z')

That would get everything that does not start with a letter. Likewise:

name BEGINSWITH[cd] '0' OR name BEGINSWITH[cd] '1' OR ... OR name BEGINSWITH[cd] '9'

Would get everything that does start with a number.

edit the comment prompted another idea:

If you could extract the first letter of the name, you could do:

NOT(name[0] IN %@), arrayOfLetters

(name[0] being the first character of name, and arrayOfLetters being an NSArray of the alphabetic characters)

懵少女 2024-10-15 22:15:22

您可以使用“匹配”,其中可以使用任何正则表达式(例如 ^[0-9])进行比较。但据我所知,这在处理核心数据时不起作用。

另一个解决方案是使用新的 (OS X 10.6, iOS 4.0) predicateWithBlock: 并使用字符串方法自行检查条件。

You can use 'MATCHES' where you can use any regex (e.g. ^[0-9]) to compare. But as far as I know that does not work when dealing with Core Data.

Another soluton would be to use the new (OS X 10.6, iOS 4.0) predicateWithBlock: and check the condition yourself with string methods.

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