将用户的电话号码转换为英文号码

时间:2014-10-13 09:28:52

标签: ios objective-c

我有一个应用程序,用户必须插入他的号码并将其发送到服务器。 我遇到的问题是有些用户用他们的母语插入他们的数字(例如乌尔都语,阿拉伯语,印度语或其他) 我想要的是将不同语言的所有数字转换为英文数字(1,2,3 ...),然后将其发送到服务器。 有可能实现这个目标吗?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

如果你不能做到,我会感到惊讶......

NSInteger blah = [enteredString intValue];
// you will have to know if it's an int, float, double, etc...
// the entered number is still a number just using a different font (I guess).
// the shape of the number 2 comes entirely from the font so I don't see why this wouldn't work

但如果这不起作用,请查看NSNumberFormatter课程。你应该可以做一些像......

NSNumberFormatter *nf = [NSNumberFormatter new];
NSNumber *number = [nf numberFromString:enteredString];

无论哪种方式都应该有效。先试试第一个。如果那不起作用,那就给数字格式化。您可能必须设置数字格式化程序的区域设置。

使用工作项目进行测试

// This is the only code.
#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self getNumber];
    return YES;
}

- (void)getNumber
{
    NSInteger number = [self.textField.text intValue];

    NSLog(@"%ld", (long)number);
}

@end

我将模拟器语言更改为阿拉伯语并且效果很好。

...截图 enter image description here

...代码 enter image description here

文字字符串与输入的字符串

我猜这是因为你的开发语言是英语。

无论如何,当您将文字字符串١‎٢‎٣输入Xcode并将其存储在字符串中时,它与从阿拉伯语文本字段中获取字符串١‎٢‎٣不同...

enter image description here

enter image description here