0

I'm very new to Xcode so excuse my unintelligence. I have an app with a text field and a button. What I am trying to achieve is inputting math into the text field, such as 2+2, and pressing the button to receive the answer, which would be 4.

  • possible duplicate of [Evaluate math expression in string? (NSString)](http://stackoverflow.com/questions/9651333/evaluate-math-expression-in-string-nsstring) – michaelsnowden Jan 02 '15 at 00:16
  • Also, for the lulz, you could do this: `- (BOOL)textFieldShouldReturn:(UITextField *)textField { [_resultLabel setText: [_webView stringByEvaluatingJavaScriptFromString: [[@"eval('" stringByAppendingString:textField.text] stringByAppendingString:@"')"]]]; return YES; }` where textField is the field the user enters into for which your UIViewController is a delegate, and _webView is a hidden UIWebView. – michaelsnowden Jan 02 '15 at 00:30

1 Answers1

1

Have a look at NSExpression which lets you do things such as:

NSExpression *expression = [NSExpression expressionWithFormat:@"4 + 5 - 2*3"];
id value = [expression expressionValueWithObject:nil context:nil]; // => 3

References

Abizern
  • 146,289
  • 39
  • 203
  • 257