I have a searchDisplayController
which works perfectly when searching for English and Arabic words using a method as follows:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSInteger)scope
{
NSString *query = self.searchDisplayController.searchBar.text;
if (query && query.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ClientName contains[cd] %@", query];
[searchResultController_.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self searchResultController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
}
However, this works fine if the iphone language is English, but if I change the iPhone language to Arabic (global settings) and tried to search in Arabic or English words the searchResultsController
will not display results, why ?
p.s. when I put a static Arabic word in query like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ClientName contains[cd] %@", @"تجريب"];
the searchDisplayController
will display the correct result of the arabic word تجريب
EDIT: I've tried to build the predicate in code like this :
NSExpression *clientNameEx=[NSExpression expressionForKeyPath:@"ClientName"];
NSExpression *aClientEx=[NSExpression expressionForConstantValue:query];
NSPredicate *predicate=[NSComparisonPredicate predicateWithLeftExpression:clientNameEx
rightExpression:aClientEx
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:0];
but to no avail ...
I've made test project with XCode 4, MasterDetail template, CoreData checkbox On and added SearchBarController to the default Master view, added UISearchBarDelegate delegate into the header, and searchBarTextDidEndEditing method to invoke search once done is pressed.
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
[NSFetchedResultsController deleteCacheWithName:nil];
self.fetchedResultsController = nil;
[self.tableView reloadData];
}
Last step was to add into fetchedResultsController:
NSString *query = self.searchDisplayController.searchBar.text;
if (query.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"timeStamp contains[cd] %@", query];
NSLog(@"predicate %@", [predicate description]);
[fetchRequest setPredicate:predicate];
}
Switched iPhone Simulator and tested it with iOS5 and Arabic settings. All searches where successful, searching for word "test" and word "تجريب" brings up results. However, word "تجريب" was copy pasted from your question, as my arabic is little bit rusted :).I've also tried to make record in database with first letter on arabic keyboard and then to search for it and result shows up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With