A
A
Abra Kadabra2016-11-02 04:02:28
iOS
Abra Kadabra, 2016-11-02 04:02:28

Is it possible in swift to evaluate the value of a string/execute a string of code?

Is it possible in swift to evaluate the value of a string?
type:
var value = evaluate("7*(10+2)")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2016-11-02
@Jmaster

OSX:

import Cocoa

var numericExpression = "7*(10+2)"
let expression = NSExpression(format: numericExpression)
var result = expression.expressionValueWithObject(nil, context: nil) as! NSNumber
print(result) // 84

iOS:
import UIKit

var numericExpression = "7*(10+2)"
let expression = NSExpression(format: numericExpression)
var result = expression.expressionValueWithObject(nil, context: nil) as! NSNumber
print(result) // 84

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question