0

I am struggling a little bit to use DDMathParser framework for expression requirement I have. I have JSON of fields & based on expressions certain fields can be set required, hidden or set the value of it.

Expressions in required tag in sample JSON are not fixed & so not getting how to achieve dynamic approach for expression.

[
  {
    "name": "firstName",
    "value": "Ameer",
    **"required": true**
  },
  {
    "name": "lastName",
    "value": "Shaikh",
    **"required": "$firstName != ‘’"**
  },
  {
    "name": "designation",
    "value": "",
    **"required": "$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"**
  },
  {
    "name": "location",
    "value": "",
    **"hidden": false**
  }
]

Actually, expression in JSON contains $ to represent one of the dictionary objects value from JSON. Wherein framework internally treats it as a local variable.

These expressions may have different combinations as well. There may be several expression apart from "required" parameters. I need to run all relevant expressions for any change in value in UI.

EDIT

let expression = "$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"
    let dict = ["firstName": "Amir", "lastName": ""]
    let e = try! Expression(string: expression)
    let result = try! Evaluator.default.evaluate(e, substitutions: dict)

Though it should parse a correct value from JSON, I have hard coded substitutions string to at least get a breakthrough. Here, substitutions expect String & Double & give error as "Cannot convert a value of type [String: String] to expected arg type substitutions (Dcitionary). Is there any way to pass String: String substitutions?

Ameer
  • 705
  • 8
  • 18
  • @Dave DeLong, Can you please give some heads up to proceed me further? – Ameer Jul 27 '17 at 05:45
  • so, what's your specific question? You have the strings there in the dictionary. DDMathParser has an initializer on `Expression` that takes a `String`... – Dave DeLong Jul 29 '17 at 16:42
  • In the third set of dictionary if you check for required field expression it is **"$firstName == ‘Ameer’ && $lastName == ‘Shaikh’"**. Where for $firstName it should read the value of "value" key in the respective dictionary. For time being I have hard coded & created substitutions dictionary. When I say Evaluator.default.evaluate(e, substitutions: dict) it gives me error saying "Cannot convert a value of type [String: String] to expected arg type substitutions (Dcitionary). What is better approach to get result with m y use case? Added sample code in post under EDIT section – Ameer Jul 31 '17 at 05:39

1 Answers1

1

DDMathParser is not built to do string evaluations. It's technically possible to make it work, but it's a bit beyond the scope of the framework.

For this situation, I think you'd be better off using NSPredicate, which does allow string comparisons and variable substitutions.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Ohhh my bad... My expression is very dynamic & may contain string, number or boolean with uncertain combinations. Can I do it by getting tokens individually & then operate on them? Or will you suggest any parallel available option, if you know any? – Ameer Aug 01 '17 at 09:43
  • I'd suggest using `NSPredicate`. `DDMathParser` is for evaluating expression as _math_, whereas `NSPredicate` is for evaluating statements that return either `true` or `false`. – Dave DeLong Aug 01 '17 at 17:38
  • Thanks Dave for your valuable inputs. – Ameer Aug 02 '17 at 06:50