Search code examples
iosobjective-cddmathparser

Solving math using DDMathParser in Objective-C


I'd like to use DDMathParser to solve a math problem. The top answer on this StackOverflow question shows how, but it only solves it to the log. I'd like to get the answer as a string on an integer. The code NSLog(@"%@", [math numberByEvaluatingString]); also works, but it only prints it to the log.

What I'd like to do would be something like answerText = @"%@", [math numberByEvaluatingString];, where "math" is the string to evaluate, and "answerText" is the string to push the answer to. However, when I try to display the string in a label like Label.text = [NSString stringWithFormat:answerText];, I get a sigabrt error. I'm not sure what's up. That code does not work at all with an integer.

Thanks in advance!


Solution

  • Try:

    label.text = [NSString stringWithFormat:@"%@", [math numberByEvaluatingString]];
    

    There is probably a better way to coerce it to a string, but I'm too lazy to go see what numberByEvaluatingString actually returns.