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.

2 Answers 2

2
let string = "1 + 2"
let numberFromString = string.numberByEvaluatingString()
printLn("\(numberFromString)")
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.