I use DDMathParser to solver formula expressions using Swift. The following code works fine, however, implicit multiplication doesn't. Reading the docs it should work... So, what do I miss here? my code:
...
substitutions.updateValue(3, forKey: "x")
let myString = "3$x"
do{
let expression = try Expression(string: myString, operatorSet: operatorSet, options: myTRO, locale: myLocale)
let result = try evaluator.evaluate(expression, substitutions: substitutions)
print("expression is: \(expression), the result is : \(result)")
} catch {
print("Error")
}
...
The code throws the "Error". Using the string "3*$x" the expression is calculated as expected.
Ok, got it myself. As Dave DeLong mentioned
.allowImplicitMultiplicationis included by default in the options but will get ignored when creating customoptions. Since I want to use localized expressions (decimal separator within expression string is local) I need to use the advanced definition ofExpression:In order to use the localized string option I defined
let myLocale = NSLocale.currentbut accidentally also created a newoperatorSetnewoptionsand passed it to the expression definition. The right way is not to create customoperatorSetandoptionsbut to use the defaults within theExpressiondefinition:Dave DeLong did a really great job in creating the
DDMatParserframework. For newbies it is very hard to get started with. The wiki section atDDMathParseris pretty basic and doesn't give some details or examples for all the other great functionalityDDMatParseris providing.