terminating with uncaught exception of type NSException in Swift 5

241 views Asked by At

My code works when my code makes etc: 2 + 2 But my app shut down when my code makes ex: 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)
    }
1

There are 1 answers

0
Andres Gomez On BEST ANSWER

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