作者:相信奇迹的发生 | 来源:互联网 | 2023-05-16 19:07
IverereadtheSwiftdocumentationanumberoftimes,butImafraidImoverlookingsomethingsimp
I've reread the Swift documentation a number of times, but I'm afraid I'm overlooking something simple here.
我已经多次阅读了Swift文档,但恐怕我忽略了一些简单的东西。
The function below processes just fine and properly calls the updateCalorieBalance method if there is value in the field caloriesConsumed.text. But, of course, crashes if caloriesConsumed.text is nil.
下面的函数可以很好地调用updateCalorieBalance方法,如果字段中有值的话。但是,当然,如果消耗了卡路里,就会崩溃。文本是零。
The error message I get is: fatal error: unexpectedly found nil while unwrapping an Optional value.
我得到的错误消息是:致命错误:在展开可选值时意外发现nil。
Your assistance is greatly appreciated.
非常感谢您的帮助。
@IBAction func buttonThree(sender: AnyObject) {
var calConsumed: String?
if let calCOnsumed= caloriesConsumed.text {
calorieCount.updateCalorieBalance(Double(calConsumed)!)
balanceLabel.text = "New Balance: \(Int((calorieCount.getCalorieBalance())))"
} else {
balanceLabel.text = "Please enter calories to add."
}
caloriesConsumed.resignFirstResponder()
}
1 个解决方案