Ios DDMathParser-如何识别错误

Ios DDMathParser-如何识别错误,ios,cocoa-touch,parsing,Ios,Cocoa Touch,Parsing,我正在构建一个应用程序,它使用Dave DeLong的DDMathParser绘制给定文本函数的图形。我需要知道(对于我计算的每个“x”)解决方案是否存在,或者它只给我0.00,因为它无法计算它。也许是个傻瓜 while (x <= (viewWidth - originOffsetX)/axisLenghtX) { NSDictionary *variableSubstitutions = [NSDictionary dictionaryWithObject: [NSN

我正在构建一个应用程序,它使用Dave DeLong的DDMathParser绘制给定文本函数的图形。我需要知道(对于我计算的每个“x”)解决方案是否存在,或者它只给我0.00,因为它无法计算它。也许是个傻瓜

while (x <= (viewWidth - originOffsetX)/axisLenghtX) {

        NSDictionary *variableSubstitutions = [NSDictionary dictionaryWithObject: [NSNumber numberWithDouble:x] forKey:@"x"];
        NSString *solution = [NSString stringWithFormat:@"%@",[[DDMathEvaluator sharedMathEvaluator] 
                                                               evaluateString:plotterExpression withSubstitutions:variableSubstitutions]];
        numericSolution = solution.numberByEvaluatingString.doubleValue;
        NSLog(@"%f", numericSolution);
        if (newline) {
            CGContextMoveToPoint(curveContext, (x*axisLenghtX + originOffsetX), (-numericSolution * axisLenghtY + originOffsetY));
            newline = FALSE;
        } else {
            CGContextAddLineToPoint(curveContext, (x*axisLenghtX + originOffsetX), (-numericSolution * axisLenghtY + originOffsetY));
        }
        x += dx;

while(x好吧,由于您使用的是尽可能最简单的API,因此如果出现错误,无法通知您。这在wiki页面的第一部分中有明确解释:

有几种方法可以计算字符串,具体取决于计算量 要进行的自定义。大多数选项都需要N错误 **参数,但有些参数不支持

  • 如果使用不接受N错误**的选项之一,则 将记录任何标记化、解析或评估错误
  • 如果使用的选项之一不接受N错误**,则 必须提供一个。否则可能导致崩溃
所以你要做的是:

NSDictionary *variableSubstitutions = [NSDictionary dictionaryWithObject: [NSNumber numberWithDouble:x] forKey:@"x"];
NSError *error = nil;
NSNumber *number = [[DDMathEvaluator sharedMathEvaluator] evaluateString:plotterExpression withSubstitutions:variableSubstitutions error:&error]];

if (number == nil) {
  NSLog(@"an error occurred while parsing: %@", error);
} else {
  numericSolution = [number doubleValue];
  // continue on normally
}

谢谢,这正是我想要的!