我正在学习对象 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;}
虽然之前已经提到过这个错误,但我看不出其他解决方案是如何应用的