Tag: nsexpression

在swift中捕获无效用户input的exception

我正在尝试这是一个计算器的代码。 我如何处理无效的用户input? //解答:将头部桥接到Objective-C // https://github.com/kongtomorrow/TryCatchFinally-Swift 这是同样的问题,但在objc,但我想迅速做到这一点。 捕获NSExpression中的NSInvalidArgumentException 所有我想要显示的是一个消息,如果它不起作用,但现在我得到一个exception,当用户没有input正确的格式。 import Foundation var equation:NSString = "60****2" // This gives a NSInvalidArgumentException', let expr = NSExpression(format: equation) // reason: 'Unable to parse the format string if let result = expr.expressionValueWithObject(nil, context: nil) as? NSNumber { let x = result.doubleValue println(x) } else { println("failed") }