I have an array of phone number objects, the phone number model is as follows:
@property NSString *hotlineName;
@property NSString *hotlineNameAr;
@property NSString *hotlineNumber;
@property NSString *hotlineImage;
@property NSInteger hotlineID;
@property RLMArray<Tags> *hotlineTags;
when I perform a search, I filter the array if either hotlineName, hotlineNameAr, hotlineNumber and in property hotlineTags: tagName and tagNameAr contain the search text.
I used NSPredicate to filter the array as such:
-(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];
}
and so far it works well, the problem arose when I search with numbers in a different locale, in this instance in Arabic, so used I NSNumberFormatter as such:
// 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];
}
but the problem with NSNumberFormatter is that any leading zero's are ignored in the conversion, so I need an alternative to format the NSString based on locale or search array while taking in consideration locale, the same option in spotlight search. I also tried formatting as such but it was in vain.
NSString *str = [[NSString alloc] initWithFormat:@"%@" locale:locale, searchText];
NSString *localizedString =[NSString localizedStringWithFormat:@"%@", searchText];