0

So I have this in my code.

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

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

It gives me an unwrapping optional value error. How do I fix it?

Chris Mikkelsen
  • 3,987
  • 9
  • 29
  • 41
  • Swift 3 Xcode version is 8 – Leo Dabus Dec 01 '16 at 12:24
  • 4
    Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Hamish Dec 01 '16 at 12:24

1 Answers1

0

You are using the wrong initializer and even if it was the right initializer the string format is wrong:

let hex = "#FFFFFF"
let newString = hex.replacingOccurrences(of: "#", with: "")
let returnValue = UInt(newString, radix:16) ?? 0
vadian
  • 274,689
  • 30
  • 353
  • 361