gpt4 book ai didi

ios - DDMathParser - 如何识别错误是 iOS

转载 作者:行者123 更新时间:2023-11-28 22:58:03 27 4
gpt4 key购买 nike

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

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;

最佳答案

好吧,由于您使用的 API 可能是最简单的,因此如果出现错误,则无法收到通知。这在 Usage 的第一部分中有清楚的解释。维基上的页面:

There are several ways to evaluate strings, depending on how much customization you want to do. Most of these options require an NSError ** parameter, although some do not.

  • If you use one of the options that does not accept an NSError **, then any tokenization, parsing, or evaluation errors will be NSLogged.
  • If you use one of the options that does accept an NSError **, then you must supply one. Failing to do so will probably result in a crash.

所以你想要做的是:

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
}

关于ios - DDMathParser - 如何识别错误是 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10418820/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com