Core Data 使用类似于 BEGINSWITH 的东西为 NSNumber 创建 NSPredicate
我正在尝试从 NSNumber 属性上的核心数据中的搜索栏文本中检索结果。 特别是,从商店获取 ID 号为“BEGINSWITH”搜索文本的对象。
举个例子,如果用户在搜索框中输入数字“1”,我获取的请求将返回包括 1、12、14、19、100、149、1324 等的对象。
不幸的是,我在表上实现排序为好吧,所以当按这个id号排序时,如果在数据模型中表示为字符串,排序就变成:1,10,100,2,20,200而不是1,2,10,20,100,200。
我怎样才能在这里获得两全其美的效果,以便我可以以字符串方式搜索,但以数字方式排序? 我是否必须在数据模型中创建两个属性,即相同数据的字符串和数字表示形式?
任何帮助将不胜感激。
平均值, 费利佩
I am trying to retrieve results from search bar text in Core Data on a property that is an NSNumber.
In particular, getting objects from the store whose id number "BEGINSWITH" the search text.
As an example, if the user enters the number "1" in the search box, my fetched request returns objects including 1, 12, 14, 19, 100, 149, 1324, etc.
Unfortunately, I am implementing sorting on the table as well, so when sorting by this id number, if represented as a string in the data model, sorting becomes: 1,10,100,2,20,200 instead of 1,2,10,20,100,200.
How can I go about getting the best of both worlds here such that I can search in a string-sort-of-way but sort in a number-sort-of-way?
Do I have to create two properties in the data model, a string and a number representation of the same data?
Any help would be much appreciated.
Rgds,
Felipe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您有两个字段,一个是数字的实际标识符,另一个是字符串的 searchIdentifier。按字符串搜索,按数字排序。
I suggest you have two fields, the actual identifier that is a number and then a searchIdentifier which is a string. Search on the string, sort on the number.
尽管 Apple 文档中没有提及,但 Like 也适用于 nsnumber。
例如,
works,其中 numberField 是 NSNumber 类型的核心数据属性。这将获取 numberfield 值以 searchTerm 开头的所有记录。
Though it is not mentioned anywhere in the apple documentation, Like works with nsnumber as well.
For example,
works, where numberField is a core data attribute of type NSNumber. This fetches all records where numberfield value begins with searchTerm.