草庐IT

expressionValue

全部标签

swift - 我可以强制 NSExpression 和 expressionValue 以某种方式假设 Doubles 而不是 Ints 吗?

我正在尝试对字符串进行数学运算。当我用NSExpression把一个字符串变成一个数学问题,然后用expressionValue得到结果时,Swift假设我想要一个整数。考虑这两个Playground示例:letcurrentCalculation="10/6"letcurrentExpression=NSExpression(format:currentCalculation)print(currentExpression)//10/6ifletresult=currentExpression.expressionValue(with:nil,context:nil)as?Doubl