Using DDMathParser to solve string in swift

566 Views Asked by At

I want to pass a string to DDMathParser and store answer in another string variable using swift. I am totally new to iOS platform so I dont really know syntax of functions. Example:

var expr = "5+9*2" //Some Expression
var result = DDMathParser.evaluate(expr) // Result of Expression
1

There are 1 best solutions below

8
Martin R On

Here is a simple example how to use DDMathParser from Swift:

let expr = "5+9*2" as NSString
let result = expr.numberByEvaluatingString().integerValue
println(result) // 23

If you need floating point results then replace integerValue by doubleValue.