0

I really need help on converting this Objective-C Code to Swift, so that I can implement it into my code. Here's the code

NSLog(@"%@", [@"1 + 2" numberByEvaluatingString]);

I know that numberByEvaluatingString isn't an actual function in Objective-C, but in my code it will work.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Epic Defeater
  • 2,107
  • 17
  • 20

2 Answers2

2
let string = "1 + 2"
let numberFromString = string.numberByEvaluatingString()
printLn("\(numberFromString)")
AnthonyMDev
  • 1,496
  • 1
  • 13
  • 27
1

There is a function to express a string as an arithmetic formula.

NSLog("%@",NSExpression(format:"1+2"))

You could also use println()

Edit: Also if you want the expression to be evaluated

let exp = NSExpression(format:"1+2")
println(exp.expressionValueWithObject(nil, context: nil))
Race B
  • 1,193
  • 1
  • 8
  • 7