NSPredicate,CoreData 中的空格。如何修剪谓词?
我有一个 CoreData/SQLite 应用程序,其中有“父类别”和“类别”。我无法控制数据,某些“父类别”值具有尾随空格。
我可以使用 CONTAINS
(或者我应该说它可以与 CONTAINS
一起使用,但这是我无法使用的)。例如,我有 2 个条目:MEN 和 MENS。如果我使用 CONTAINS
我将返回两条记录,您可以看到这将是一个问题。
我可以轻松地在我这边进行修剪,但谓词会将其与数据库进行比较,并且不会匹配。所以我的问题是,如果可能的话,如何解释谓词中的空格。
我有一个类别“MENS”,有人在应用程序中选择了该类别,并将其与数据库中的“MENS”进行比较。
I have a CoreData/SQLite application in which I have "Parent Categories" and "Categories". I do not have control over the data, some of the "Parent Categories" values have trailing white spaces.
I could use CONTAINS
(or I should say it works with CONTAINS
but this is something I can not use). For example I have 2 entries, MEN and MENS. If I use CONTAINS
I will return both records, you can see how this would be an issue.
I can easily trim on my side, but the predicate will compare that with the database and will not match. So my question is how can I account for whitespaces in the predicate, if possible at all.
I have a category "MENS" which someone has selected in the application, and it is compared against "MENS " in the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在查找之前修剪数据。您可以使用
stringByTrimmingCharactersInSet
轻松完成此操作。通过提前执行此操作,您还可以避免任何性能影响。如果您要与CONTAINS
进行基于字符的比较,这可能会很昂贵。因此,假设您的搜索字符串是“MEN”。
以下是删除任何可疑字符的方法:
还有
whitespaceAndNewlineCharacterSet
,它可以按照罐头上的说明执行操作。或者,您也可以轻松创建自己想要修剪的内容的自定义特征。
为此,请查看:
NSCharacterSet 类参考
和
Apple 的 字符串编程指南< /a>
I would trim the data prior to doing the lookup. You can do this easily using
stringByTrimmingCharactersInSet
. By doing it beforehand, you'll also avoid any performance hit. That could be expensive if you're doing a character based comparison withCONTAINS
.So, let's say your search string is "MEN".
Here's the way to strip out any dodgy characters:
There's also
whitespaceAndNewlineCharacterSet
which does what it says on the tin.Alternatively, it's easy to create your own custom character of stuff you want to trim.
For that, have a look at:
NSCharacterSet Class Reference
and
Apple's String Programming Guide