views:

253

answers:

1

Hi everyone, I'm trying to make a predicate that includes special characters

For example:

[[myIngredients filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"name BEGINSWITH[c] %@", [alphabet objectAtIndex:idx]]];

Here I will get all the ingredient which starts with (let say for idx = 5) 'e'. As I have to do my app in english and french, some ingredients start with special character like 'é' or even 'œ' for 'o'. How can I include these special characters in my predicate?

Best

+1  A: 

I think you might be looking for the “diacritic insensitive” flag that NSPredicate supports. It’s just like the “c” flag you’re already using, except you use a “d”. Like so:

… predicateWithFormat:@"name BEGINSWITH[cd] %@", …

Now the string “e” will also match “é”, “ê”, “ë”, and so on.

Todd Yandell
Thank you, it works like a charm... except for the œ! Do you have any nice solution for this one? Otherwise, I'll add an exception.
ncohen
The only thing I can think of is using a compound predicate. Create one NSPredicate to match “o” and another to match “œ”. Then join them together with: `[NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:p1, p2, nil]]`.
Todd Yandell