Reputation: 2113
I am downloading some values from a cloud service as JSON objects and assigning them to NSString objects as below
NSString *price = [orpObjectDict objectForKey:@"price"];
NSString *qty = [orpObjectDict objectForKey:@"qty"];
My debug output shows that both strings have values as below
NSLog(@"price: %@ <--> qty: %@", price, qty);
2017-04-17 16:29:05.665043 NWMobileTill[1490:730225] price: 6.95 <--> qty: 1
But the following creation of NSDecimalNumber using those NSStrings fail
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
Why is this simple creation crashing?
Upvotes: 1
Views: 799
Reputation: 2207
You are getting Number and you are write code as string
Please write as this code in string formate
NSString *price = [NSString stringWithFormat:@"%@", orpObjectDict objectForKey:@"price"];
NSString *qty = [ [NSString stringWithFormat:@"%@",orpObjectDict objectForKey:@"qty"];
or simply check
if [price isKindOfClass:[NSString class]]
{ // It is string
}
else if [price isKindOfClass:[NSNumber class]]
{
// It is number
}
Upvotes: 2
Reputation: 1043
You can directly retrieve your object by keys using 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);
the output is:
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
You can always check return object types with isKindOfClass
or isMemberOfClass
the latter being more restrictive.
As a rule of thumb, make sure the dictionary contains the keys and its value it's not nil
before assigning:
id value = dictionary[key];
if (value) {...}
or for a better check:
if (value && value != [NSNull null]) {...}
EDIT
I think Objective C types management is weaker than Swift's. Anyway using [NSDecimalNumber alloc] initWithString:
method gives you the right instance.
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");
}
Upvotes: -1
Reputation: 285069
The error message
[__NSCFNumber length]: unrecognized selector sent to instance 0x174226240
is pretty clear: It reveals that the object for key price
is an NSNumber
object.
So you have to create an NSDecimalNumber
from an NSNumber
:
NSNumber *price = orpObjectDict[@"price"];
NSDecimalNumber *priceNumber = [NSDecimalNumber decimalNumberWithDecimal:price.decimalValue];
Upvotes: 0