我正在建立一个应用程序,图形的文本给定函数使用戴夫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;发布于 2012-05-03 01:38:55
好吧,因为你使用的是最简单的API,所以如果有错误,就没有办法通知你。这在维基上的Usage页面的第一部分中有明确的解释:
有几种方法可以评估字符串,具体取决于您要执行的自定义操作。这些选项中的大多数都需要NSError **参数,但也有一些不需要。
所以你要做的是:
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
}https://stackoverflow.com/questions/10418820
复制相似问题