Reputation: 11
I have a table:
let CTCSStable:[String] = [ "67.0 ", "69.3 ", "71.9 "]
I need to convert a selected entry to be a string equal to ten times the numeric value of the entry.
var tempCTCSS:String = self.CTCSStable[ctcssIndex]
let tempCTCSSF:Float = Float(tempCTCSS)!
This throws an exception:
fatal error: unexpectedly found nil while unwrapping an Optional value
Upvotes: 0
Views: 1868
Reputation: 2281
The reason you are getting the crash is because Float(tempCTCSS)!
is trying to convert tempCTCSS
into a Float, but it fails, and then you force unwrap that value and it is nil, so it crashes.
The reason why it fails to convert that string to a Float is because there is white space. Try removing the spaces.
I would never force unwrap (i.e. !
) unless you know that that value is there. The best practice would be to wrap that value in an if let
statement. That conditionally unwraps the value and prevents the crash
Upvotes: 4