You might be better off using a 3rd party expression parser/evaluator,
such as DDMathParser.
NSExpression
is quite limited, and has no options to force floating
point evaluation.
If you want to (or have to) stick to NSExpression
: Here is a possible solution to (recursively) replace all constant
values in an expression by their floating point value:
extension NSExpression {
func toFloatingPoint() -> NSExpression {
switch expressionType {
case .constantValue:
if let value = constantValue as? NSNumber {
return NSExpression(forConstantValue: NSNumber(value: value.doubleValue))
}
case .function:
let newArgs = arguments.map { $0.map { $0.toFloatingPoint() } }
return NSExpression(forFunction: operand, selectorName: function, arguments: newArgs)
case .conditional:
return NSExpression(forConditional: predicate, trueExpression: self.true.toFloatingPoint(), falseExpression: self.false.toFloatingPoint())
case .unionSet:
return NSExpression(forUnionSet: left.toFloatingPoint(), with: right.toFloatingPoint())
case .intersectSet:
return NSExpression(forIntersectSet: left.toFloatingPoint(), with: right.toFloatingPoint())
case .minusSet:
return NSExpression(forMinusSet: left.toFloatingPoint(), with: right.toFloatingPoint())
case .subquery:
if let subQuery = collection as? NSExpression {
return NSExpression(forSubquery: subQuery.toFloatingPoint(), usingIteratorVariable: variable, predicate: predicate)
}
case .aggregate:
if let subExpressions = collection as? [NSExpression] {
return NSExpression(forAggregate: subExpressions.map { $0.toFloatingPoint() })
}
case .anyKey:
fatalError("anyKey not yet implemented")
case .block:
fatalError("block not yet implemented")
case .evaluatedObject, .variable, .keyPath:
break // Nothing to do here
}
return self
}
}
Example:
let expression = NSExpression(format: "10/6+3/4")
if let result = expression.toFloatingPoint().expressionValue(with: nil, context: nil) as? Double {
print("result:", result) // 2.41666666666667
}
This works with "simple" expressions using arithmetic operators and functions and some "advanced" expression types (unions, intersections, ...). The remaining conversions can be added if necessary.
NSExpression(forFunction: function, arguments: newArgs)
withNSExpression(forFunction: operand, selectorName: function, arguments: newArgs)
. Otherwise you'll break expressions that invoke a selector on a value. – Tantalic