为什么NSDecimalNumber的创建会崩溃?

时间:2017-04-17 08:37:00

标签: ios objective-c nsdecimalnumber

我从云服务下载一些值作为JSON对象,并将它们分配给NSString对象,如下所示

NSString *price = [orpObjectDict objectForKey:@"price"];
NSString *qty = [orpObjectDict objectForKey:@"qty"];

我的调试输出显示两个字符串的值都低于

NSLog(@"price: %@ <--> qty: %@", price, qty);

2017-04-17 16:29:05.665043 NWMobileTill[1490:730225] price: 6.95 <--> qty: 1

但是下面使用NSStrings创建NSDecimalNumber失败

NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithString:price];
NSDecimalNumber *qtyNumber = [NSDecimalNumber decimalNumberWithString:qty];

017-04-17 16:29:05.665886 NWMobileTill[1490:730225] -[__NSCFNumber length]: unrecognized selector sent to instance 0x174226240
2017-04-17 16:29:05.668080 NWMobileTill[1490:730225] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x174226240'
*** First throw call stack:
(0x1926fd1b8 0x19113455c 0x192704268 0x192701270 0x1925fa80c 0x19323658c 0x1932340f0 0x193234754 0x1000d9078 0x192cff1e8 0x192d17120 0x1931ebfb0 0x193130aa8 0x1931210a4 0x1931ee35c 0x1006c5218 0x1006d2aec 0x1006c8ce0 0x1006d4e2c 0x1006d4b78 0x19178f2a0 0x19178ed8c)
libc++abi.dylib: terminating with uncaught exception of type NSException

为什么这个简单的创作会崩溃?

3 个答案:

答案 0 :(得分:1)

您正在获取Number,并且您将代码编写为字符串

请在字符串格式中编写此代码

 NSString *price = [NSString stringWithFormat:@"%@", orpObjectDict objectForKey:@"price"];
 NSString *qty = [ [NSString stringWithFormat:@"%@",orpObjectDict objectForKey:@"qty"];

或只是检查

if [price isKindOfClass:[NSString class]]
{  // It is string
}
else if [price isKindOfClass:[NSNumber class]]
{
  // It is number
}

答案 1 :(得分:0)

错误消息

  

[__ NSCFNumber length]:无法识别的选择器发送到实例0x174226240

非常明确:它揭示了密钥price的对象是NSNumber对象。

因此,您必须从NSDecimalNumber创建NSNumber

NSNumber *price = orpObjectDict[@"price"];
NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithDecimal:price.decimalValue];

答案 2 :(得分:-1)

您可以使用NSDecimalNumber直接通过键检索对象:

NSDictionary*orpObjectDict = @{
                               @"price" : @(5.4),
                               @"qty" : @(2)
                               };
NSString *price = [orpObjectDict objectForKey:@"price"];
NSLog(@"string price %@", price);

NSString *qty = [orpObjectDict objectForKey:@"qty"];
NSLog(@"string qty %@", qty);

NSDecimalNumber*dPrice = [orpObjectDict objectForKey:@"price"];
NSLog(@"decimal price %@", dPrice);

NSDecimalNumber *dqty = [orpObjectDict objectForKey:@"qty"];
NSLog(@"decimal quantity %@", dqty);

输出是:

2017-04-17 10:46:12.932 test[2836:50653] string price 5.4
2017-04-17 10:46:12.932 test[2836:50653] string qty 2
2017-04-17 10:46:12.933 test[2836:50653] decimal price 5.4
2017-04-17 10:46:12.933 test[2836:50653] decimal quantity 2

您始终可以使用isKindOfClassisMemberOfClass检查返回对象类型,后者更具限制性。

根据经验,在分配之前,请确保字典包含密钥及其值nil

id value = dictionary[key];
if (value) {...}

或更好的检查:

if (value && value != [NSNull null]) {...}

修改

我认为Objective C类型管理比Swift更弱。无论如何使用[NSDecimalNumber alloc] initWithString:方法为您提供正确的实例。

NSDictionary*orpObjectDict = @{
                               @"price" : @"5.4",
                               @"qty" : @(2)
                               };
NSString* price = [orpObjectDict objectForKey:@"price"];
NSLog(@"string price %@", price);

NSDecimalNumber*decimalPrice = [[NSDecimalNumber alloc] initWithString:price];
if ([decimalPrice isMemberOfClass:NSDecimalNumber.class]) {
    NSLog(@"dqty is a NSDecimalNumber");
}