简体   繁体   English

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

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

My code works when my code makes etc: 2 + 2 But my app shut down when my code makes ex: 2+2+ . 我的代码在执行以下代码时可以正常工作: 2 + 2但是,当我的代码进行例如2 + 2 +时,我的应用程序将关闭。
I tried to do - catch code block and many error handling. 我试图做-捕获代码块和许多错误处理。 I can't solve the problem. 我无法解决问题。

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)
    }

Maybe something like this 也许像这样

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.

相关问题 Swift 2以NSException类型的未捕获异常终止 - Swift 2 terminating with uncaught exception of type NSException Swift中的“终止为NSException类型的未捕获异常” - “terminating with uncaught exception of type NSException” in Swift Swift:使用未捕获的异常类型NSException终止应用程序 - Swift: Terminating app with uncaught exception type NSException 以 NSException SWIFT 类型的未捕获异常终止 - terminating with uncaught exception of type NSException SWIFT Swift项目以NSException类型的未捕获异常终止 - Swift Project Terminating with uncaught exception of type NSException Swift:使用NSException类型的未捕获异常终止 - Swift: Terminating with uncaught exception of type NSException 在Swift中以NSException类型的未捕获异常终止 - terminating with uncaught exception of type NSException in Swift SWIFT:使用Facebook登录时,“终止于NSException类型的未捕获异常” - SWIFT: “terminating with uncaught exception of type NSException” when sign in with facebook 使用类型NSException定时器Swift崩溃的未捕获异常终止 - Terminating With Uncaught Exception Of Type NSException Timer Swift Crash GIDSignInButton - 使用NSException类型的未捕获异常终止 - GIDSignInButton - Terminating with uncaught exception of type NSException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM