如果 iphone 语言不是英语,searchDisplayController 不会显示结果

发布于 2024-12-25 12:59:45 字数 1745 浏览 1 评论 0原文

我有一个 searchDisplayController ,它在使用如下方法搜索英语和阿拉伯语单词时完美工作:

- (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
    }      
}

但是,如果 iphone 语言是英语,则效果很好,但如果我将 iPhone 语言更改为阿拉伯语(全局)设置)并尝试用阿拉伯语或英语单词搜索 searchResultsController 不会显示结果,为什么?

ps 当我在查询中放置一个静态阿拉伯单词时,如下所示: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ClientName contains[cd] %@", @"Ьурийб"]; searchDisplayController 将显示阿拉伯语单词的正确结果 编辑:我尝试在这样的代码

中构建谓词:

    NSExpression *clientNameEx=[NSExpression expressionForKeyPath:@"ClientName"];
    NSExpression *aClientEx=[NSExpression expressionForConstantValue:query];
    NSPredicate *predicate=[NSComparisonPredicate predicateWithLeftExpression:clientNameEx
                                                             rightExpression:aClientEx 
                                                                    modifier:NSDirectPredicateModifier
                                                                        type:NSContainsPredicateOperatorType
                                                                     options:0];

但无济于事......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2025-01-01 12:59:46

我已经使用 XCode 4、MasterDetail 模板、CoreData 复选框打开了测试项目,并将 SearchBarController 添加到了默认的主视图中,将 UISearchBarDelegate 委托添加到了标题中,并在按下完成后调用了 searchBarTextDidEndEditing 方法来调用搜索。

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    [NSFetchedResultsController deleteCacheWithName:nil];
    self.fetchedResultsController = nil;
    [self.tableView reloadData];
}

最后一步是添加到 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 并使用 iOS5 和阿拉伯语设置进行测试。所有搜索都是成功的,搜索单词“test”和单词“TÀ׬лий”会显示结果。
然而,单词“TÀ׬ַև”是从您的问题中复制粘贴的,因为我的阿拉伯语有点生锈了:)。我还尝试在数据库中使用阿拉伯语键盘上的第一个字母进行记录,然后搜索它并显示结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文