JamesDon
JamesDon

Reputation: 75

Coredata fetch request using NSPredicate

I want to do a phone number search in sqlite db. Phone table have a number field which holds a string value (eg: +1 (2-34)56-78 ).

If user enters search text as "234", then I need to fetch the phone numbers with only digits(eliminating all other alphabets or special characters in the string stored) using NSPredicate and compare it with the entered string. Should I do this using a predicate in a fetch request?

I tried with following predicate and it doesn't return me the expected result:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"contactName != null AND SUBQUERY(phoneList, $phone, ANY $phone.number CONTAINS[c] %@).@count > 0",searchText];

IS there any way to get this done? Can I use regular expression with NSPredicate for this?

Upvotes: 0

Views: 104

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70936

NSPredicate doesn't support that kind of matching. Your Core Data model should include data that your app requires for filtering-- which might mean extra fields that are not normally visible to users. If you need to match numbers without regard to non-numeric characters, you should add a field which contains that data. If the actual string is +1 (2-34)56-78, add a secondary field where you store 12345678. Use this extra field in the predicate.

Upvotes: 1

Related Questions