6

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 ...

JAHelia
  • 6,934
  • 17
  • 74
  • 134
  • 1
    Use searchText instead of the text in searchBar. And please, NSLog() the received string in searchText and compare with the literal string that works fine as you say in your p.s. So we will know if the problem is with that string. – Gabriel Jan 09 '12 at 09:59
  • I have done this, the NSLog shows the same literal arabic string in the p.s. section of my question, BUT what drives me crazy is that the result is empty !! – JAHelia Jan 09 '12 at 10:20
  • And what if you compare both strings with ´[@"تجريب" compare:searchText]´ ? What does it return? NSOrderSame? – Gabriel Jan 09 '12 at 11:23
  • yes it returns NSOrderedSame ... this is soo weird – JAHelia Jan 09 '12 at 11:32
  • 1
    Then in my opinion the problem is predictaWithFormat: is not working well. I have found some bugs with predicateWithFormat: before. Try to build the predicate in code, with NSComparisonPredicate. – Gabriel Jan 09 '12 at 13:55
  • I have NSLog'ed the predicate object in both cases too, it displays the exact same predicate in console. how can i make the above predicate using the NSComparisonPredicate ? – JAHelia Jan 09 '12 at 14:05
  • @ Gabriel: look at EDIT above – JAHelia Jan 10 '12 at 06:22
  • 1
    I cannot help with this. The fact that inserting a literal string identical to the one you are using, and if you are using now searchText instead of the text in searchBar, is enough for what I know. I only can think about some special "compare" system in fetch that we don't know. For example, a need to make all strings canonical before searching (I mean using decomposedStringWithCanonicalMapping for example) if it don't search for equivalent unicodes but for identical codifications. – Gabriel Jan 10 '12 at 08:20

1 Answers1

1

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.

Alex
  • 2,468
  • 1
  • 20
  • 16
  • i did the exact same thing as you did but didn't display any result when the iphone language is Arabic – JAHelia Jan 13 '12 at 10:14
  • random thought: does it matter if your development machine's language is xyz? have you tried changing it? – govi Jan 13 '12 at 12:20
  • When project was created, together with the database, language of the simulator was set to English, only afterwards I changed it to Arabic. Perhaps that's the case? Have you tried to copy&paste "تجريب" or type it on simulator? In my case, Mac is set to English. – Alex Jan 13 '12 at 16:18
  • my case is like yours exactly, and yes I copied the word and typed it in the simulator but to no avail .. – JAHelia Jan 15 '12 at 07:12