0

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

0

1 Answer 1

4

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.

  1. The reason why it fails to convert that string to a Float is because there is white space. Try removing the spaces.

  2. 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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.