简体   繁体   中英

NSPredicate for NSNumber Contains NSNumber (as NSString)

Simple approach: I have a SearchView where the user can select some CoreDate and he can search for Name , City (both NSString ) and Clientnumber ( NSNumber )

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"Name contains[cd] %@ OR City contains[cd] %@",
                                searchText,searchText];

This is my predicate for Name and City, but how can I search for a searchquery within an NSNumber field?

So if I would search for 300 , it should return entries with Clientnumber : 300123 and 1230012, because both contains the 300 .

So, how can I do this? Is it possible to "convert" the value for Clientnumber within the NSPredicate to NSString ?

You should be able to use CAST(Clientnumber, 'NSString') contains[cd] %@ to do this search:

NSPredicate *resultPredicate = [NSPredicate
                            predicateWithFormat:@"Name contains[cd] %@ OR City contains[cd] %@ OR CAST(Clientnumber, 'NSString') contains[cd] %@",
                            searchText,searchText,searchText];

I found that dasblineklight's answer wouldn't work for me in a core data search using an NSFetchedResultsController . What DID work however was passing number.description as the name of the property, so for this specific case that would be:

NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"Name contains[cd] %@ OR City contains[cd] %@ OR Clientnumber.description contains[cd] %@",
                                searchText, searchText, searchText];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM