CoreData 谓词:字符串属性长度? [英] CoreData predicate: string property length?

查看:27
本文介绍了CoreData 谓词:字符串属性长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个实体 Fragment,它有一个属性 'text',它是一个字符串,我想查询 Fragment 的列表,它的 text 长度为 5:

Say if I have an entity Fragment, it has an attribute 'text' which is a string, I want to query the list of Fragment whose text is of length 5:

[NSPredicate predicateWithFormat:@"position == %@ AND text.length == %d", pos, 5];

它不起作用(即不返回任何结果),但是如果我在查询中删除 text.length 它起作用并且我确定存在长度为 5 的文本,那么我需要将其更改为什么?

It does not work (ie returns no result), but if I remove text.length in the query it works and I'm certain that there are texts of length 5, so what do I need to change it to?

谢谢!

推荐答案

NSPredicate 中没有字符串的长度属性.改用正则表达式.您的谓词应如下所示:

There is no length attribute for strings in the NSPredicate. Use regex instead. Your predicate should look as follows:

[NSPredicate predicateWithFormat:@"position == %@ AND text MATCHES %@", pos, @".{5}"];

这篇关于CoreData 谓词:字符串属性长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆