What is the use of NSExpression?

后端 未结 4 1873
野趣味
野趣味 2021-01-03 13:23

How to calculate formulas like (a + b) ^ 2, sum((a + b) / 2) using NSExpression? I am the beginner to NSExpression.

(a + b) ^

4条回答
  •  被撕碎了的回忆
    2021-01-03 13:46

    NSExpression is used to represent expressions in NSPredicate. It is not intended for the tasks like yours.

    Actually, you can use NSExpression to calculate the formula value, but this usage is quite limited. To evaluate any mathematical expression you would need to make a string parser using some algorithm i.e. Shunting-yard or just reuse a library like this one.

提交回复
热议问题