如何创建 NSPredicate 来查找具有前导数值的条目?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 MATCHES 运算符不适用于获取请求,那么您实际上必须手动执行操作:
这将获取不以字母开头的所有内容。同样:
将获得以数字开头的所有内容。
编辑评论引发了另一个想法:
如果您可以提取
name
的第一个字母,您可以这样做:(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:That would get everything that does not start with a letter. Likewise:
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:(
name[0]
being the first character ofname
, andarrayOfLetters
being anNSArray
of the alphabetic characters)您可以使用“匹配”,其中可以使用任何正则表达式(例如 ^[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.