Throw NSInvalidArgumentException from NSExpression with Swift

I use NSExpression to solve mathematical formulas. Everthing works fine if the original formula is correct, but if I enter a formula like:

6- 

My application ends with the following error message:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "6- == 1"' 

In Object-C, this code can be placed in the @try block. Is there anything equal in speed? Or how do I handle these Exceptions?

Here is my simple NSExpression code:

 let expression = NSExpression(format: finalFormula) if let result = expression.expressionValueWithObject(nil, context: nil) as? NSNumber { return result } 

I would decide to check the input first, but I would be fine for me if I go back to zero or something like that.

+6
source share

Source: https://habr.com/ru/post/981165/


All Articles