So, I've been making a basic calculator. But I've run into an issue. My code.
@IBAction func calculate(_ sender: UIButton) {
let stringExpression = resultLabel.text!
let expression = NSExpression(format: stringExpression)
let result = expression.expressionValue(with: nil, context: nil) as! NSNumber
resultLabel.text! = String(result.doubleValue) // Writes response on textLabel :D
}
The code above works. However, when a input such as 5++6 is entered it crashes my app. I'm not very sure how to deal with this.
The following error is shown when a invalid input is entered.
The error given.
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I'm not sure how I could make it display "Error". And this should work for any type of error.