首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DDMathParser - How to recognize error is iOS

DDMathParser - How to recognize error is iOS
EN

Stack Overflow用户
提问于 2012-05-03 01:33:16
回答 1查看 180关注 0票数 0

我正在建立一个应用程序,图形的文本给定函数使用戴夫DeLong的DDMathParser。我需要知道(对于我计算的每个"x“)解是否存在,或者它只给我0.00,因为它不能计算它。也许是布尔牌?

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-03 01:38:55

好吧,因为你使用的是最简单的API,所以如果有错误,就没有办法通知你。这在维基上的Usage页面的第一部分中有明确的解释:

有几种方法可以评估字符串,具体取决于您要执行的自定义操作。这些选项中的大多数都需要NSError **参数,但也有一些不需要。

  • 如果您使用的选项之一不接受NSError **,则任何标记化、解析或求值错误都将为NSLogged。
  • 如果您使用接受NSError **的选项之一,则必须提供一个。如果不这样做,可能会导致系统崩溃。

所以你要做的是:

代码语言:javascript
复制
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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10418820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档