Epic Defeater
Epic Defeater

Reputation: 2147

Code Conversion from Objective-C to Swift

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.

Upvotes: 0

Views: 97

Answers (2)

Race B
Race B

Reputation: 1203

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))

Upvotes: 1

AnthonyMDev
AnthonyMDev

Reputation: 1506

let string = "1 + 2"
let numberFromString = string.numberByEvaluatingString()
printLn("\(numberFromString)")

Upvotes: 2

Related Questions