我已经在 obj-c 上玩了几个月了,对很多东西都感觉很舒服,但无法理解看起来很简单的东西。我需要能够从 NSString 拆分为一个数组,然后从该数组中获取 NSDecimalNumbers。如果我尝试执行任何计算,我会得到一个“无法识别的选择器发送到实例”。如果我简单地打印每个十进制数字,一切都会顺利进行。如果我将值手动放入 NSDecimalNumbers 而不是从也可以工作的数组中提取。错误和代码如下。
2013-10-14 15:54:42.174 测试[30829:c07]-[__NSCFString decimalNumberByAdding:]:无法识别的选择器发送到实例 0x75a81f0 2013-10-14 15:54:42.176 测试[30829:c07] * 由于应用程序终止uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString decimalNumberByAdding:]: unrecognized selector sent to instance 0x75a81f0' * First throw call stack: (0x1c90012 0x10cde7e 0x1d1b4bd 0x1c7fbbc 0x1c7f94e 0x2075 0x10e1705 0x152c0 0x15258 0xd6021 0xd657f 0xd56e8 0x44cef 0x44f02 0x22d4a 0x14698 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x11ffc 0x1bfd 0x1b25) libc++abi.dylib:终止调用抛出异常
-(IBAction)buttonPushed: (id) sender
{
NSString* s = @"25.55, 109.24";
NSArray* a = [s componentsSeparatedByString: @" "];
NSDecimalNumber* dec1 = [a objectAtIndex: 0];
NSDecimalNumber* dec2 = [a objectAtIndex: 1];
NSDecimalNumber* sum = [dec1 decimalNumberByAdding: dec2];
statusText.text = [NSString stringWithFormat: @"%@", sum, nil];
}