Clone this wiki locally
There are two of ways to evaluate strings, depending on how much customization you want to do.
let value = try "1 + 2".evaluate()
evaluate() method on
String is intended for the most basic use-cases. While it does not "swallow" any errors, it offers very little in the way of customization. If your string contains variables, you can optionally pass in a substitutions dictionary:
let value = try "$foo + 2".evaluate(["foo": 40])
The other approach is using the
Evaluator object, which requires an
let expression = try Expression(string: "1 + 2") let evaluator: Evaluator = ... let value = try evaluator.evaluate(expression)
When evaluating with an
Evaluator, you have access to most of the high-level customizability options of
DDMathParser. For example, prior to evaluation you may change the default angle measurement mode:
evaluator.angleMeasurementMode = .Degrees
To access the full range of customizability, including customizing the behavior of the
% operator, defining your own operators, or adding new tokens for existing operators, you'll customize the creation of the
Expression object itself. For more information, see the Advanced Usage page.