Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate the string equation in ios

Is there way to solve the string equations in ios ?

For example

Input:

NSString * str =@"1+2";

Output:

NSInteger result = 3 // i.e sum of 1+2 from str

How to go about this and get expected result!

Please help!

like image 914
Vinayak Kini Avatar asked May 28 '13 07:05

Vinayak Kini


1 Answers

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.

like image 175
Amin Negm-Awad Avatar answered Oct 05 '22 04:10

Amin Negm-Awad