繁体   English   中英

在Swift 5中以NSException类型的未捕获异常终止

[英]terminating with uncaught exception of type NSException in Swift 5

我的代码在执行以下代码时可以正常工作: 2 + 2但是,当我的代码进行例如2 + 2 +时,我的应用程序将关闭。
我试图做-捕获代码块和许多错误处理。 我无法解决问题。

let islem: String = screenTextfield.text!
let exp: NSExpression = NSExpression(format: islem)
if let result: Double = exp.expressionValue(with: nil, context: nil) as? Double{
        islemLabel.text = String(result)
    }

也许像这样

var islem: String = screenTextfield.text!

if let number = Int(String(islem.last!)) {
   print(number)
}
else {
   islem.removeLast()
}

let exp = NSExpression(format: islem)
if let result = exp.expressionValue(with: nil, context: nil) as? NSNumber {
    islemLabel.text = String(result.doubleValue)
} 
else {
    makeAlert(title: "Error", message: "Wrong math type")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM