应用程序因未捕获的异常“类的问题”而终止

时间:2013-01-09 04:58:57

标签: objective-c xcode

我正在学习对象C,我无法弄清楚为什么会出现以下错误:

2013-01-08 21:46:12.984 ObjectiveCDemo[38355:c07] -[Calculator subtractFromTotal:]: unrecognized selector sent to instance 0x7568c90
2013-01-08 21:46:12.985 ObjectiveCDemo[38355:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Calculator subtractFromTotal:]: unrecognized selector sent to instance 0x7568c90'

我正在创建一个执行计算器功能的简单应用。我正在使用的代码是:

    calculator = [[Calculator alloc] init];
    double sum = [calculator addToTotal:10];
    double difference = [calculator subtractFromTotal:6];
    double results = [calculator multiplyTimesTotal:3];
    double quotient = [calculator divideFromTotal:4];
    [calculator clear];

    NSLog(@"Sum: %f", sum);
    NSLog(@"Difference: %f", difference);
    NSLog(@"Results: %f", results);
    NSLog(@"Quotient: %f", quotient);

双差异线上发生错误。我可以注释掉该行和引用它的NSlog行,程序运行正常。计算器类的示例是:

- (double) addToTotal:(double)value{total += value;
return total;}

- (double) subtractfromTotal:(double)value{total -= value;
return total;}

- (double) multiplyTimesTotal:(double)value{total *= value;
return total;}

- (double) divideFromTotal:(double)value{total /= value;
return total;}

虽然之前已经提到过这个错误,但我看不出其他任何解决方案是如何应用的

2 个答案:

答案 0 :(得分:1)

您的方法名称与您调用它的方式不匹配。

致电代码:

double difference = [calculator subtractFromTotal:6];

实现:

- (double) subtractfromTotal:(double)value

应该是:

- (double) subtractFromTotal:(double)value

编译器无疑警告过你。你应该注意编译器警告!他们通常会告诉你一些重要的事情!

答案 1 :(得分:0)

仔细查看错误日志,您可以看到此行

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Calculator subtractFromTotal:]: unrecognized selector sent to instance 0x7568c90'

它清楚地告诉您,当您使用subtractFromTotal:(double)value实施邮件时,尝试使用subtractfromTotal:(double)value发送邮件。注意错误日志。