开发者

Core Data creating NSPredicate for NSNumber using something akin to BEGINSWITH

I am trying to retrieve results from search bar text in Core Data on a pro开发者_如何学JAVAperty 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


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.


Though it is not mentioned anywhere in the apple documentation, Like works with nsnumber as well.

For example,

[NSPredicate predicateWithFormat:@"numberField like '%@*'", searchTerm]

works, where numberField is a core data attribute of type NSNumber. This fetches all records where numberfield value begins with searchTerm.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜