ios - Swift string formula into a real calculation -


i have several formulas stored in plist such * b. i'm trying figure out how take formula stored string in plist , use actual calculation formula. tried going route of making formula \(a) * \(b) , setting , b before trying use formula did not work. suggestions?

example

let = 5 let b = 2  println (formula) 

actually printed out "\(a) * \(b)"

xcode 8.3.1 • swift 3.1

extension string {     var expression: nsexpression {         return nsexpression(format: self)     } }  let = 5 let b = 2 let intdictionary = ["a": a,"b": b]  var formula = "a * b" if let timesresult = formula.expression.expressionvalue(with: intdictionary, context: nil) as? int {       print(timesresult) // 10 } formula = "(a + b) / 2" if let intavgresult = formula.expression.expressionvalue(with: intdictionary, context: nil) as? int {     print(intavgresult)    // 3 }    let x = 5.0 let y = 2.0 let z = 3.0  let doubledictionary = ["x": x, "y": y, "z": z]   formula = "(x + y + z) / 3" if let doubleavgresult = formula.expression.expressionvalue(with: doubledictionary, context: nil) as? double {     print(doubleavgresult) } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -