0

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
Bilal
  • 558
  • 8
  • 18

1 Answers1

0

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • It gave me an exception. Shouldn't i be using NSlog function. because in C-Obj i saw that statement is executed through that function. But i am unable to get it working in swift. – Bilal May 10 '15 at 11:35
  • @Bilal: Any error messages? I tried that code and it worked for me. – Martin R May 10 '15 at 11:36
  • Following exception: [__NSCFString numberByEvaluatingString]: unrecognized selector sent to instance 0x7f9212dcc1e0 2015-05-10 16:40:43.826 Test-1[956:13800] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString numberByEvaluatingString]: unrecognized selector sent to instance 0x7f9212dcc1e0' – Bilal May 10 '15 at 11:41
  • Its v long actually cant copy paste all of it. – Bilal May 10 '15 at 11:42
  • That probably means that you did not add the DDMathParser files to your target. – Martin R May 10 '15 at 11:45
  • But i have imported it. It shows in intellisense if I write DD it auto completes it to DDMathParser. – Bilal May 10 '15 at 11:49
  • Are all the .h and .m files in your project? Is the "Target Membership" checkbox in the File inspector switched on for *all* .m files? – Martin R May 10 '15 at 11:57
  • That checkbox is switched and full DDMathParser folder that is included in my project. But in bridging file only DDMathParser.h file is imported. Could this be a problem? – Bilal May 10 '15 at 12:00