swiftnsexpression

Swift String into formula


This may be the wrong place to ask this but I am aware of NSExpression, but it seems it can only do primitive mathematics - am I wrong about this? I am able to do something like this: @Value-2/3+9-6 ect easily, but I would like to incorporate Max's, Min's, and possible a few other operations (instead of just multiplication, division, subtraction, and addition. Is that possible in the same equation, does it have to be converted multiple times? Any advice would be appreciated!


Solution

  • I figured it out.

    For anyone who needs this - you can take any string and use:

    functionName(x) for most of the functions like sqrt, multiplyby, trunc, ceiling, ect

    And then for the six with multiple variables (max, min, count, average, sum, ect) you use functionName({x,y}).

    All can be used in a string with expressionValue(with: nil, context: nil)