如何忽略前缀“A”和“该”使用 NSFetchedResultsController 排序时?
我正在使用 CoreData 来保存 SQLite 数据库中保存的书籍信息。按标题排序时,我想忽略“A”和“The”前缀。例如,当有书:“The Code Book”和“Fermat's Enigma”时,我希望“Fermat's Enigma”出现在“The Code Book”之前,因为“The”应该被忽略。
除了添加一个持久属性来保存没有前缀的标题之外,我找不到任何方法可以做到这一点。 (在上面的示例中,该属性“titleNoPrefixes”应包含“Code Book”字符串)。在 CoreData 中,基于计算属性(又名瞬态属性)的排序似乎是不可能的。
添加持久属性显然是一个糟糕的数据库设计,但我看不到其他方法。有什么建议吗?
I am using CoreData to hold books information saved in SQLite database. When sorting by title, I would like to ignore the "A" and "The" prefixes. For example, when having the books: "The Code Book" and "Fermat's Enigma", I wish "Fermat's Enigma" to come before "The Code Book" as "The" should be ignored.
I couldn't find any way to do that besides adding a persistent property that will hold the title without prefixes. (In the example above, that property say 'titleNoPrefixes' should hold "Code Book" string). Sorting based on calculated properties (aka, transient properties) seems to be impossible in CoreData.
Adding a persistent property is obviously a bad database design but I can't see another way. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Joshua,
你的问题几乎与 Core Data 作为持久存储无关,这是一个关于文本问题的一般数据库排序。解决问题的传统方法是使用一个用作排序类别的范式字段。您可能想要小写所有内容并删除该字段的干扰词。由于这些只是标题,因此存储方面并不是特别昂贵。此外,这是插入时间操作而不是搜索时间操作。最后,瞬态变量仅存在于 RAM 中。我相信,它们在数据存储中是 NULL 值。因此,搜索它们是毫无意义的,并且会引发异常。
安德鲁
Joshua,
Your problem has almost nothing to do with Core Data as a persistent store, this is a general database sorting on text problem. The traditional solution to your problem is to have a normal form field that is used as the sort category. You might want to lower case everything and strip your noise words for this field. As these are just titles, this isn't particularly expensive, storage wise. Furthermore, this is an insert time operation rather than a search time op. Finally, transient variables only exist in RAM. They are NULL values, I believe, in the datastore. Hence, searching on them is pointless and throws an exception.
Andrew
您可以编写自己的 NSString 类别来实现:
You can write your own NSString category that implements: