-1

我正在学习对象 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;}

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

4

2 回答 2

1

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

调用代码:

double difference = [calculator subtractFromTotal:6];

执行:

- (double) subtractfromTotal:(double)value

应该:

- (double) subtractFromTotal:(double)value

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

于 2013-01-09T05:02:54.880 回答
0

仔细查看错误日志,你可以看到这一行

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

subtractFromTotal:(double)value它清楚地告诉您,当您使用 实现它时,您正试图发送一条消息subtractfromTotal:(double)value。注意错误日志。

于 2013-01-09T05:12:32.193 回答