Can you evaluate a string in Swift?
No.
There is no equivalent of eval()
in JavaScript or ScriptEngine
in Java for Swift.
A common use for string evaluation is mathematical expressions; if you want to evaluate these, you can use NSExpression.valueWithExpression(format: String)
:
let stringWithMathematicalOperation: String = "5*5" // Example
let exp: NSExpression = NSExpression(format: stringWithMathematicalOperation)
let result: Double = exp.expressionValue(with:nil, context: nil) as! Double // 25.0