swift - Optional value error after updating to Xcode 6 GM -


i have updated xcode 6 gm , fixing issues have appeared. things working have encountered error unsure how rectify.

i have code in viedidload():

    currencyformatter.numberstyle = nsnumberformatterstyle.currencystyle     currencyformatter.currencycode = nslocale.currentlocale().displaynameforkey(nslocalecurrencysymbol, value: nslocalecurrencycode)! 

i error on second line.

fatal error: unexpectedly found nil while unwrapping optional value 

i'm sure solution simple i'm pretty new programming haven't been able find fix.

you're using wrong method currency code. wouldn't problem, method returns nil because couldn't find value. explicitly unwrapped optional adding !, should avoided.

i suggest following code:

currencyformatter.numberstyle = nsnumberformatterstyle.currencystyle  let locale = nslocale.currentlocale() if let currencycode = locale.objectforkey(nslocalecurrencycode) as? string {     currencyformatter.currencycode = currencycode } 

this sets currency code if returned objectforkey method. pob pointed out correctly in comments below, objectforkey returns anyobject?. in case it's safe add as? string, casts string?.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -