xcode - iOS App Crashes on Phone but works fine on simulator -


i have following code:

var displayvalue: double{     get{         println("display.text =\(display.text!)")         return nsnumberformatter().numberfromstring(display.text!)!.doublevalue     }     set{         display.text = "\(newvalue)"         userisinthemiddleoftypinganumber = false;     } } 

it works fine in simulator. when try on phone crashes. here console:

digit= 3 display.text =3 operandstack =[3.0] digit= 2 display.text =2 operandstack =[3.0, 2.0] display.text =6.0 fatal error: unexpectedly found nil while unwrapping optional value 

this line:

nsnumberformatter().numberfromstring(display.text!)!

is returning nil causing app crash cause couldn't unwrap optional. don't know what's wrong. i'm following tutorials in itunes u.

any appreciated.

try:

get{     println("display.text =\(display.text!)")     let formatter = nsnumberformatter()     formatter.locale = nslocale(localeidentifier: "en_us_posix")     return formatter.numberfromstring(display.text!)!.doublevalue } 

because, nsnumberformatter uses devices locale default, it's possible decimal separator not ".". example:

let formatter = nsnumberformatter() formatter.locale = nslocale(localeidentifier: "ar-sa") print(formatter.decimalseparator!) // -> outputs "٫" formatter.numberfromstring("6.0") // -> nil 

the formatter uses such locales cannot parse strings "6.0". if want consistent result formatter, should explicitly specify locale.

as en_us_posix locale, see the document:

in cases best locale choose en_us_posix, locale that's designed yield english results regardless of both user , system preferences. en_us_posix invariant in time (if us, @ point in future, changes way formats dates, en_us change reflect new behavior, en_us_posix not), , between platforms (en_us_posix works same on iphone os on os x, , on other platforms).


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -