在NSArray中搜索具有不同语言环境的对象

时间:2016-03-31 10:11:02

标签: objective-c search nsstring nspredicate nslocale

我有一系列电话号码对象,电话号码型号如下:

@property NSString *hotlineName;
@property NSString *hotlineNameAr;
@property NSString *hotlineNumber;
@property NSString *hotlineImage;
@property NSInteger hotlineID;
@property RLMArray<Tags> *hotlineTags;

当我执行搜索时,如果hotlineName,hotlineNameAr,hotlineNumber和属性hotlineTags:tagName和tagNameAr包含搜索文本,我会过滤数组。

我使用NSPredicate来过滤数组:

 -(void) searchForText: (NSString *) searchText{

  NSString *predicateFormat = @"SELF.%K contains[cd] %@";
  NSString *tagFormat = @"ANY SELF.hotlineTags.%K contains[cd] %@";

  NSString *searchNameAttribute = @"hotlineName" ;
  NSString *searchTagAttribute = @"tagName";
  NSString *searchNameAttribute_ar = @"hotlineNameAr" ;
  NSString *searchTagAttribute_ar = @"tagNameAr";
  NSString *numberAttribute = @"hotlineNumber";

  NSPredicate *namePredicate = [NSPredicate  predicateWithFormat:predicateFormat, searchNameAttribute, searchText];

  NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:tagFormat, searchTagAttribute, searchText];

  NSPredicate *namePredicate_ar = [NSPredicate predicateWithFormat:predicateFormat, searchNameAttribute_ar, searchText];

  NSPredicate *tagPredicate_ar = [NSPredicate predicateWithFormat:tagFormat, searchTagAttribute_ar, searchText];

  NSPredicate *numberPredicate = [NSPredicate predicateWithFormat:predicateFormat, numberAttribute, searchText];

  NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[namePredicate, numberPredicate, tagPredicate, namePredicate_ar, tagPredicate_ar]];

  filteredResults = [hotlines_arr filteredArrayUsingPredicate:predicate]; 
}

到目前为止它运行良好,当我用不同的语言环境搜索数字时出现问题,在这个例子中是阿拉伯语,因此我使用了NSNumberFormatter:

// Convert string From Arabic/Persian numbers to English numbers
+(NSString *) convertToEnglishNumber:(NSString *) string {

    // NSNumericSearch
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"EN"];
    [formatter setLocale:locale];
    NSNumber *number = [formatter numberFromString:string];

    return [number stringValue];

}

但是NSNumberFormatter的问题在于转换中忽略了任何前导零,所以我需要一种替代方法来根据区域设置或搜索数组格式化NSString,同时考虑区域设置,聚光灯搜索中的相同选项。 我也尝试过格式化,但这是徒劳的。

NSString *str = [[NSString alloc] initWithFormat:@"%@" locale:locale, searchText];

NSString *localizedString =[NSString localizedStringWithFormat:@"%@", searchText];

1 个答案:

答案 0 :(得分:0)

由于我无法想到任何其他选项,这是最后的选择,我最终更换了字符串,但我对答案非常不满意:

+ (NSString *) replaceStrings: (NSString *) textToEdit{
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"ar"];
    for (NSInteger i= 0; i < 10; i++) {
        NSString *stringVal = [@(i) stringValue];
        NSString *localeString = [@(i) descriptionWithLocale:locale];
        textToEdit = [textToEdit stringByReplacingOccurrencesOfString:localeString withString:stringVal];
    }
    return textToEdit;
}
相关问题