As Tim mentions, we can use a regular expression to match text containing i or ı . I also did not want to add a new field or change the source data, since the search distorts a huge number of lines. So I decided to use regular expressions and NSPredicate .
Create an NSString category and copy this method. It returns the base matching pattern of or . You can use it with any method that accepts a regular expression pattern.
- (NSString *)zst_regexForTurkishLettersWithCaseSensitive:(BOOL)caseSensitive { NSMutableString *filterWordRegex = [NSMutableString string]; for (NSUInteger i = 0; i < self.length; i++) { NSString *letter = [self substringWithRange:NSMakeRange(i, 1)]; if (caseSensitive) { if ([letter isEqualToString:@"ı"] || [letter isEqualToString:@"i"]) { letter = @"[ıi]"; } else if ([letter isEqualToString:@"I"] || [letter isEqualToString:@"İ"]) { letter = @"[Iİ]"; } } else { if ([letter isEqualToString:@"ı"] || [letter isEqualToString:@"i"] || [letter isEqualToString:@"I"] || [letter isEqualToString:@"İ"]) { letter = @"[ıiIİ]"; } } [filterWordRegex appendString:letter]; } return filterWordRegex; }
So, if the search word is Şırnak , it creates Ş[ıi]rnak for case sensitivity and Ş[ıiIİ]rnak for case insensitive search.
And here are the possible use cases.
NSString *testString = @"Şırnak"; // First create your search regular expression. NSString *searchWord = @"şır"; NSString *searchPattern = [searchWord zst_regexForTurkishLettersWithCaseSensitive:NO]; // Then create your matching pattern. NSString *pattern = searchPattern; // Direct match // NSString *pattern = [NSString stringWithFormat:@".*%@.*", searchPattern]; // Contains // NSString *pattern = [NSString stringWithFormat:@"\\b%@.*", searchPattern]; // Begins with // NSPredicate // c for case insensitive, d for diacritic insensitive NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self matches[cd] %@", pattern]; if ([predicate evaluateWithObject:testString]) { // Matches } // If you want to filter an array of objects NSArray *matchedCities = [allAirports filteredArrayUsingPredicate: [NSPredicate predicateWithFormat:@"city matches[cd] %@", pattern]];
You can also use NSRegularExpression , but I think that using case and diacritical insensitive searches using NSPredicate much easier.
irmakcanozsut
source share