Swift TextField使用阿拉伯语键盘输入数字

Swift TextField使用阿拉伯语键盘输入数字,swift,input,int,uitextfield,arabic,Swift,Input,Int,Uitextfield,Arabic,我们在应用程序中使用的输入很少,使用的是数字键盘类型。我们将该值存储为Int 几天前,我们的一个应用程序用户使用阿拉伯语键盘时遇到了一个错误。当试图将输入保存到数据库时,我们遇到了错误,因为输入是阿拉伯符号,不能识别为Int 我想知道你们是怎么解决这种问题的。我想这样转换输入值,所以我为String类创建了一个小扩展: func convertToNumber() -> Int { let locale = NSLocale(localeIdentifier: "EN")

我们在应用程序中使用的输入很少,使用的是数字键盘类型。我们将该值存储为Int

几天前,我们的一个应用程序用户使用阿拉伯语键盘时遇到了一个错误。当试图将输入保存到数据库时,我们遇到了错误,因为输入是阿拉伯符号,不能识别为Int

我想知道你们是怎么解决这种问题的。我想这样转换输入值,所以我为String类创建了一个小扩展:

func convertToNumber() -> Int {

    let locale = NSLocale(localeIdentifier: "EN")
    let formatter = NSNumberFormatter()
    formatter.locale = locale

    let number = formatter.numberFromString(self)
    let returnNumber = Int(number!)

    return returnNumber
}

还有其他更好的方法吗?

我使用下面的代码将阿拉伯数字“١٢٣٤٦٧٨٩٠”转换为英文数字“1234567890”。我将值作为字符串返回,所以可能这就是区别所在

如果仍然需要返回Int值,请尝试更改代码:

let returnNumber = Int(number!)

这是我的工作代码:

extension String {
func arabicNumberToEnglish() -> String{

    //if number start with zero then Formatter.number(from: self) will remove it, 
   //so we need to know how many leading zero contain in the string 
   //so that we can add them before the return
    var leadingZero: String = ""

    func getLeadingZero(_ value: String)
    {

        if value.hasPrefix("0") || value.hasPrefix("٠") {
            leadingZero = leadingZero + "0"
            var newValue = value
            newValue = String(newValue.characters.dropFirst())
            getLeadingZero(newValue)
        }
        else{
            return
        }


    }

    //calling the inline function to get the leading zeros count
    getLeadingZero(self)

    let Formatter: NumberFormatter = NumberFormatter()
    Formatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
    if let final = Formatter.number(from: self){
        // returning the final result after adding the leadingZero's to it.
        return leadingZero + "\(final)"

    }
    else{
        return self
    }

}

}

我使用下面的代码将阿拉伯数字“١٣٤٥٦٧٨٩٠”转换为英语数字“1234567890”,我将返回字符串形式的值,因此可能这就是差异所在

如果仍然需要返回Int值,请尝试更改代码:

let returnNumber = Int(number!)

这是我的工作代码:

extension String {
func arabicNumberToEnglish() -> String{

    //if number start with zero then Formatter.number(from: self) will remove it, 
   //so we need to know how many leading zero contain in the string 
   //so that we can add them before the return
    var leadingZero: String = ""

    func getLeadingZero(_ value: String)
    {

        if value.hasPrefix("0") || value.hasPrefix("٠") {
            leadingZero = leadingZero + "0"
            var newValue = value
            newValue = String(newValue.characters.dropFirst())
            getLeadingZero(newValue)
        }
        else{
            return
        }


    }

    //calling the inline function to get the leading zeros count
    getLeadingZero(self)

    let Formatter: NumberFormatter = NumberFormatter()
    Formatter.locale = NSLocale(localeIdentifier: "EN") as Locale!
    if let final = Formatter.number(from: self){
        // returning the final result after adding the leadingZero's to it.
        return leadingZero + "\(final)"

    }
    else{
        return self
    }

}

}

您可以将输入文本限制为数字。一种方法是实现
UITextFieldDelegate
并实现
textField:shouldChangeCharactersRange:replacementString:
,然后检查传入的strign是否是数字(不是文本,不是任何其他unicode等),并相应地返回bool。您可以将输入文本限制为数字。一种方法是实现
UITextFieldDelegate
并实现
textField:shouldChangeCharactersRange:replacementString:
,然后检查传入的strign是否是数字(不是文本,不是任何其他unicode等)返回bool同样,你应该添加一些解释,解释为什么这应该起作用-你也可以添加代码以及代码本身中的注释-在其当前形式中,它没有提供任何解释,可以帮助社区其他人理解你为解决/回答问题做了什么。对于较老的问题和已经有答案的问题,这一点尤其重要。您应该真正添加一些解释,说明为什么应该这样做-您还可以添加代码以及代码本身中的注释-以其当前形式,它没有提供任何可以帮助社区其他人理解您为解决/回答问题所做的解释。这对于较老的问题和已经有答案的问题尤为重要。