0

不久前我看到过一个类似的话题,但这似乎是因为方程在某处使用了 NSNumber。我检查了我所有的代码,我只能看到我正在为我的方程式使用 NSDecimalNumbers。

我有一个返回一些定价数字的 JSON 网络服务。

在对象属性中解析它们之后,我想用它们运行以下等式:

NetPrice * ( 1 + (RawPercentage / 100)) + FixedPrice = processesNetPrice

更新:我将代码更改为:

    NSDecimalNumber *one = [NSDecimalNumber one];

    if (!self.netPrice || [self.netPrice isKindOfClass:[NSNull class]]) 
    { self.netPrice = [NSDecimalNumber zero]; }

    NSDecimalNumber *nettoPrijs = self.netPrice;
    NSLog(@"self.netPrice = %@",self.netPrice);
    //Returns: self.netPrice = 89.25

    NSLog(@"nettoPrijs = %@",nettoPrijs);
    //Returns: nettoPrijs = 89.25

    if (!self.userData.BandFixed || [self.userData.BandFixed isKindOfClass:[NSNull class]]) 
    { self.userData.BandFixed = [NSDecimalNumber zero]; }

    NSDecimalNumber *fixedPrice = self.userData.BandFixed;

    NSLog(@"self.userData.BandFixed = %@",self.userData.BandFixed);
    //Returns: self.userData.BandFixed = 0

    NSLog(@"fixedPrice = %@",fixedPrice);
    //Returns: fixedPrice = 0

    if (!self.userData.BandPercentage || [self.userData.BandPercentage isKindOfClass:[NSNull class]]) 
    { self.userData.BandPercentage = [NSDecimalNumber zero]; }

    NSDecimalNumber *rawPercentage = self.userData.BandPercentage;

    NSLog(@"self.userData.BandPercentage = %@",self.userData.BandPercentage);
    //Returns: self.userData.BandPercentage = 0

    NSLog(@"rawPercentage = %@",rawPercentage);
    //Returns: rawPercentage = 0

    NSDecimalNumber *percentage = [rawPercentage decimalNumberByMultiplyingByPowerOf10:-2];
    NSLog(@"percentage = %@",percentage);
    //Returns: percentage = 0

    NSDecimalNumber *onePercent = [one decimalNumberByAdding:percentage];
    NSLog(@"onePercent = %@",onePercent);
    //Returns: onePercent = 1

    NSDecimalNumber *onePercentTimesNetPrice = [onePercent decimalNumberByMultiplyingBy:nettoPrijs];
    NSLog(@"onePercentTimesNetPrice = %@",onePercentTimesNetPrice);
    //Returns: onePercentTimesNetPrice =        
    -0.000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000359498933794611654993903616

    NSDecimalNumber *addingFixed = [onePercentTimesNetPrice decimalNumberByAdding:fixedPrice];
    NSLog(@"addingFixed = %@",addingFixed);
    //Returns: addingFixed =  
    -0.000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000359498933794611654993903616

如果我改变:

NSDecimalNumber *onePercentTimesNetPrice = [onePercent decimalNumberByMultiplyingBy:nettoPrijs];

到:

NSDecimalNumber *onePercentTimesNetPrice = [nettoPrijs decimalNumberByMultiplyingBy:onePercent];

它崩溃了:

-[__NSCFNumber decimalNumberByMultiplyingBy:]: unrecognized selector sent to instance 0x84ad160
4

2 回答 2

1

正如您的 NSLog() 输出所示,self.userData.RawPercentage并且self.userData.FixedPricenil,即未分配 + 初始化为NSDecimalNumber对象。

nil不能用来表示十进制数零!

事实上,当我尝试你的代码时,

NSDecimalNumber *custPrice1 = [oneWithNet decimalNumberByAdding:priceFixed];

崩溃是因为priceFixed它是 nil。

因此,您应该检查初始化这些变量的代码,并确保它们指向有效NSDecimalNumber对象。


更新:错误消息

-[__NSCFNumber decimalNumberByMultiplyingBy:]: unrecognized selector sent to instance 0x84ad160

表明这nettoPrijs是一个NSNumber对象而不是一个NSDecimalNumber。以下简化代码显示了如果您尝试将 aNSDecimalNumber与 a相乘会发生什么NSNumber

NSDecimalNumber *one = [NSDecimalNumber one];
id price = [NSNumber numberWithFloat:89.25];
NSDecimalNumber *product = [one decimalNumberByMultiplyingBy:price];
NSLog(@"product = %@", product);
// Output:
// product = -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000221166721562906606351581184

(我不知道为什么这不会崩溃或产生一些错误消息。)如果你乘以 a ,它可以正常工作NSDecimalNumber

price = [NSDecimalNumber decimalNumberWithString:@"89.25"];
product = [one decimalNumberByMultiplyingBy:price];
NSLog(@"product = %@", product);
// Output:
// product = 89.25

所以我假设这self.netPrice是一个NSNumber必须转换为NSDecimalNumber. 这同样适用于其他数字对象。

于 2012-12-10T13:19:46.670 回答
0

我使用以下方法修复了它:

- (void) doMath
{
    NSDecimalNumber *one = [NSDecimalNumber one];
    NSLog(@"Band: %@ one = %@",self,one);
    if (!self.netPrice || [self.netPrice isKindOfClass:[NSNull class]]) { self.netPrice = [NSDecimalNumber zero]; }
    NSDecimalNumber *nettoPrijs = self.netPrice;
    NSLog(@"self.netPrice = %@",self.netPrice);
    NSLog(@"nettoPrijs = %@",nettoPrijs);
    if (!self.userData.BandFixed || [self.userData.BandFixed isKindOfClass:[NSNull class]]) { self.userData.BandFixed = [NSDecimalNumber zero]; }
    NSDecimalNumber *fixedPrice = self.userData.BandFixed;
    NSLog(@"self.userData.BandFixed = %@",self.userData.BandFixed);
    NSLog(@"fixedPrice = %@",fixedPrice);
    if (!self.userData.BandPercentage || [self.userData.BandPercentage isKindOfClass:[NSNull class]]) { self.userData.BandPercentage = [NSDecimalNumber zero]; }
    NSDecimalNumber *rawPercentage = self.userData.BandPercentage;
    NSLog(@"self.userData.BandPercentage = %@",self.userData.BandPercentage);
    NSLog(@"RawPercentage = %@",rawPercentage);
    NSDecimalNumber *percentage = [rawPercentage decimalNumberByMultiplyingByPowerOf10:-2];
    NSLog(@"percentage = %@",percentage);
    NSDecimalNumber *onePercent = [one decimalNumberByAdding:percentage];
    NSLog(@"onePercent = %@",onePercent);

NSDecimalNumber gertje = [NSDecimalNumber decimalNumberWithDecimal:nettoPrijs.decimalValue]; * NSDecimalNumber *onePercentTimesNetPrice = [gertje decimalNumberByMultiplyingBy:onePercent];

    NSLog(@"onePercentTimesNetPrice = %@",onePercentTimesNetPrice);
    NSDecimalNumber *addingFixed = [onePercentTimesNetPrice decimalNumberByAdding:fixedPrice];
    NSLog(@"addingFixed = %@",addingFixed);
}

感谢你的帮助!

于 2012-12-11T10:55:39.683 回答