views:

232

answers:

2

I have an array of dictionaries. I would like to filter that array by seeing if the @"name" field of each dictionary contains a given string.

The catch is that I would like to make my filtering insensitive to case and diacritics.

If the array contained only strings I could easily use an NSPredicate. However, it doesn't, and I don't see a way that NSPredicate can accomodate this situation.

If I only cared about case-insensitivity, I could loop through all the items and compare the lowercased filter string to the lowercased name. But I don't know of a similar trick for diacritics.

Is there a good solution to this somewhere?

+1  A: 

Check the top answer on this question:

http://stackoverflow.com/questions/2167857/2168962#2168962

You should be able to use that code to get rid of the diacritics and then do a case insensitive compare or search.

St3fan
brilliant, works great. Thanks!
Mike
+1  A: 

What about something like:

NSArray * array = .....
NSString * searchString = @"foo";
NSArray * filteredArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name contains[cd] %@", searchString]];
Dave DeLong
Ah, I didn't realize you could do that. Nice. It actually won't work for my particular case because I over-simplified the example -- the array contains a mix of dictionaries and strings, so the predicate would have to have language to distinguish between the two cases somehow, which I suspect is beyond its capability. This seems like a more graceful solution than @St3fan's though, so I'll try it next time I run into this problem.
Mike
@Mike one way to "hack" it would be to add a category on NSString that defines `-[NSString name]`, that just `return self;` That way you could execute a predicate looking for `name` on both dictionaries and strings.
Dave DeLong