Stanford Calculator app keeps crashing

前端 未结 2 2002
生来不讨喜
生来不讨喜 2021-01-25 07:56

I\'m taking the Stanford iPad and iPhone developer course online at Stanford using Swift and working on creating a Calculator application. (Still a bit new to programming.)

2条回答
  •  春和景丽
    2021-01-25 08:09

    "found nil while unwrapping an Optional" means that you have a variable that may or maynot have a value, when you use the operator ! you are telling swift "Trust me there is a value in there" however if there is not swift will throw a exception as the one you just saw.

    The best way to avoid this is checking before use:

    if let value = display.text{
        //if your code get here value is safe to use
        NSNumberFormatter().numberFromString(value).doubleValue
    }
    

提交回复
热议问题