0

I am stuck at a piece of code where I need to get the expression result based on the passed dictionary. For example: expressionString = "(val1 + val2)" How it is possible to pass these values from an dictionary or somewhere else. I searched many places but does't work for me.

let expressionString : String = "(val1 + val2)" as String
let expression = NSExpression(format: expressionString)

In above case I would like to know how I can map these val1 and val2 data by passing any argument.

Is it can be achieve by let ex = NSExpression(format: expressionString, argumentArray:[parameter]) ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anupam Gupta
  • 623
  • 1
  • 7
  • 18
  • Is this https://stackoverflow.com/a/46931871/1187415 what you are looking for? – Martin R Jun 12 '18 at 18:07
  • Or this one https://stackoverflow.com/a/30855597/1187415 ? – Martin R Jun 12 '18 at 18:16
  • Fine!, It helps me but I need to perform some changes at my end. No issue but sometime if the required values are not present in the passed argument then it will get crash, and we don't have ant option to handle the crash, does any one else faces the same issue..? – Anupam Gupta Jun 14 '18 at 14:31
  • let expression = NSExpression(format: stringExpression!) let result = expression.expressionValue(with: variableDict, context: nil) as? NSNumber – Anupam Gupta Jun 14 '18 at 14:32
  • NSExpression is quite limited in its functionality and error handling. But there are other expression parsers/evaluators, such as DDMathParser. – Martin R Jun 15 '18 at 17:09

0 Answers0