How to evaluate the string equation in ios

后端 未结 6 1272
一向
一向 2020-12-15 19:48

Is there way to solve the string equations in ios ?

For example

Input:

NSString * str =@\"1+2\";

Output:

<

6条回答
  •  被撕碎了的回忆
    2020-12-15 19:55

    You can use NSExpression for this:

    NSExpression *expression = [NSExpression expressionWithFormat:@"1+2"];
    NSLog(@"%@", [expression expressionValueWithObject:nil context:nil]);
    

    For further information read the documentation of the used methods.

提交回复
热议问题