如果 iphone 语言不是英语,searchDisplayController 不会显示结果
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经使用 XCode 4、MasterDetail 模板、CoreData 复选框打开了测试项目,并将 SearchBarController 添加到了默认的主视图中,将 UISearchBarDelegate 委托添加到了标题中,并在按下完成后调用了 searchBarTextDidEndEditing 方法来调用搜索。
最后一步是添加到 fetchedResultsController:
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.
Last step was to add into fetchedResultsController:
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.