2016-12-01 62 views
0

所以我在我的代碼中有這個。如何解開可選值?

let hex:String = "#FFFFFF" 
var returnValue = UInt() 
var newString = String() 

newString = hex.replacingOccurrences(of: "#", with: "0x") 
returnValue = UInt(newString)! //This line gets an error 

它給了我一個解包可選值錯誤。我如何解決它?

+0

斯威夫特3的Xcode版本是8 –

+4

的[什麼是「致命的錯誤可能重複:意外發現零而展開一個可選值「的意思?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Hamish

回答

0

您使用了錯誤的初始化即使它是正確的初始化字符串格式錯誤:

let hex = "#FFFFFF" 
let newString = hex.replacingOccurrences(of: "#", with: "") 
let returnValue = UInt(newString, radix:16) ?? 0