1

I'm trying to implement searching for my app. If You write an exact word in the search bar, it's ok but for example if I want to find "book" and I type in "böök"diacritics shouldn't matter. I was using the code below but it's not useful and also fails while searching e.g "New York - Björk". I also tried different combinations of searchEditedText but the code became disgusting. How can I do that?

NSString *searchText = [searchBar.text capitalizedString];
NSString *searchEditedText=[searchBar.text capitalizedString];
NSMutableArray *searchArray = [[[NSMutableArray alloc] init] autorelease];
for (NSDictionary *dictionary in listOfItems) {
  NSArray *array = [dictionary objectForKey:@"Names"];
  [searchArray addObjectsFromArray:array];
}
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"u" withString:@"ü"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"c" withString:@"ç"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"U" withString:@"Ü"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"C" withString:@"Ç"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"o" withString:@"ö"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"O" withString:@"Ö"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"g" withString:@"ğ"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"G" withString:@"Ğ"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"s" withString:@"ş"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"S" withString:@"Ş"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"i" withString:@"ı"];
searchEditedText = [searchEditedText stringByReplacingOccurrencesOfString:@"İ" withString:@"I"];
for (NSString *sTemp in searchArray) {
  NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
  NSRange titleResultsRange2 = [sTemp rangeOfString:searchEditedText options:NSCaseInsensitiveSearch];
  if (titleResultsRange2.length > 0 || titleResultsRange.length > 0) {
    [copyListOfItems addObject:sTemp];
  }
}
dda
  • 6,030
  • 2
  • 25
  • 34
David
  • 217
  • 1
  • 2
  • 10

3 Answers3

4

If you're using Core Data, you can use an NSPredicate that searches in a case and diacritic insensitive manner. The answers in this question are performing case insensitive searches , if you add [cd] as part of the comparison e.g. contains[cd] or ==[cd], then it will perform a case and diacritic insensitive search.

Community
  • 1
  • 1
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • I'm not using Core Data, I looked that answer But I think It doesn't solve "o-ö, u-ü,...etc"? Nevertheless I tried, It didn't work. – David Jul 14 '12 at 11:06
  • Well the NSPredicate search only works for Core Data, so using it on something that isn't that will simply not work. You should expand on your question with details of the mechanism you're using to search for information already i.e. is the data stored in a sqlite database or something else, and what mechanism you are currently using to search for data. – Anya Shenanigans Jul 14 '12 at 12:58
  • Thank you, I expanded my question. And also If There is a solution of "writing 'böök', finding 'book'" with Core Data, I can use it no problem. – David Jul 14 '12 at 13:46
2

In fact, you could do like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Names CONTAINS[cd] %@",searchText];

searchArray = [[listOfItems filteredArrayUsingPredicate:predicate] mutableCopy];

Because NSPredicate can ignore the unicode,then, the searchArray is what you want.

Paradise
  • 558
  • 6
  • 17
1

Did you try just taking your existing code and replacing all instances of

NSCaseInsensitiveSearch

with

NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch|NSWidthInsensitiveSearch

? This seems like it would solve your problem in the most direct way possible.

If you want böök to match book but not vice versa, then see this answer for a way to strip the diacritical marks off of böök before searching for it. But that's error-prone, and probably not what you really want to do anyway.

Community
  • 1
  • 1
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159