我有一个名为的属性total
,它是一个NSDecimalNumber
. 在我的 switch 语句中,我试图对数字进行一些简单的加法和减法,但我不断收到“ unrecognized selector sent to instance
”错误。这是代码:
switch (t) {
case 0:
{
if (_zero == YES)
{
_total = [_total decimalNumberBySubtracting:[NSDecimalNumber decimalNumberWithString:@"4.5"]];
i = (UIImageView *)[self.view viewWithTag:7];
i.hidden = YES;
} else {
NSDecimalNumber *fourpointfive = [[NSDecimalNumber alloc] initWithDecimal:[[NSNumber numberWithFloat:4.5] decimalValue]];
_total = [_total decimalNumberByAdding:fourpointfive];
i = (UIImageView *)[self.view viewWithTag:7];
i.hidden = NO;
}
break;
在同样的方法中,就在 switch 语句之前,我初始化它:
_total = [[NSDecimalNumber alloc] initWithDecimal:[[NSNumber numberWithFloat:0] decimalValue]];
在我的@interface 中:
@property (strong, nonatomic) NSDecimalNumber *total;
更新:
将初始化更改为:
_total = [[NSDecimalNumber alloc] initWithString:@"0"];
但它仍然崩溃。还删除了对decimalValue
.
这是我收到的错误消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber decimalNumberByAdding:]: unrecognized selector sent to instance