swift - TextField number input using Arabic keyboard -


we using few inputs in our app use number pad type of keyboard. storing values int.

few days ago encountered error 1 of our app users uses arabic keyboard. when trying save input database encounter error since input in arabic symbols , doesn't recognised int.

i wonder how guys solve kind of problems. thinking of converting input value created little extension string class:

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 } 

is there other better approach?

i used code below convert arabic numbers "١٢٣٤٥٦٧٨٩٠" english numbers "1234567890" i'm returning value string maybe difference.

if still need return int value try change code:

let returnnumber = int(number!) 

to

if int(number!) != nil {  return int(number!) } else{  return -1 //here can return value can check decide    if error happen or no. } 

here working code:

extension string { func arabicnumbertoenglish() -> string{      //if number start 0 formatter.number(from: self) remove it,     //so need know how many leading 0 contain in string     //so can add them before 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 inline function leading zeros count     getleadingzero(self)      let formatter: numberformatter = numberformatter()     formatter.locale = nslocale(localeidentifier: "en") locale!     if let final = formatter.number(from: self){         // returning final result after adding leadingzero's it.         return leadingzero + "\(final)"      }     else{         return self     }  }  } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -