使用Swift处理来自NSExpression的NSInvalidArgumentExceptionexception

我使用NSExpression来解决数学公式。 如果输入公式正确,则Everthing工作正常,但如果我输入如下公式:

6- 

我的应用程序因以下错误消息而终止:

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

在Object-C中,可以将此代码放入@try块中。 swift中有相同的东西吗? 或者我该如何处理此exception?

这是我简单的NSExpression代码:

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

我解决方案是首先检查输入,但如果我回到零或类似的东西,我会对我好。