Very strange NSDecimalNumber precision error (or perhaps operator error that I'm not seeing) with RestKit v 0.10.2

Class definition:

....
@property (nonatomic, assign) NSInteger menuItemId;
@property (nonatomic, strong) NSString *menuItemTitle;
@property (nonatomic, strong) NSString *menuItemDescription;
@property (nonatomic, strong) NSDecimalNumber *menuItemPrice;
....

JSON text:

{"MenuItemId":5,
 "MenuItemTitle":"Grilled Jumbo Shrimp Penne Pasta",
 "MenuItemDescription":"spinach, mushrooms, garlic, sour cream, sun-dried tomatoes",
 "MenuItemBasePrice":9.9500}

Object mapping is unremarkable:

 [objectMapping mapKeyPath:@"MenuItemId" toAttribute:@"menuItemId"];
 [objectMapping mapKeyPath:@"MenuItemTitle" toAttribute:@"menuItemTitle"];
 [objectMapping mapKeyPath:@"MenuItemDescription" toAttribute:@"menuItemDescription"];
 [objectMapping mapKeyPath:@"MenuItemBasePrice" toAttribute:@"menuItemPrice"];

Yet deserialization results in menuItemPrice value = 9.9499999999

Seems that error / issue can be trace back to these lines in RKObjectMappingOperation in method

- (id)transformValue:(id)value atKeyPath:(NSString *)keyPath toType:(Class)destinationType

....
} else if ([sourceType isSubclassOfClass:[NSNumber class]] && [destinationType isSubclassOfClass:[NSDecimalNumber class]]) {
    // Number -> Decimal Number
    return [NSDecimalNumber decimalNumberWithDecimal:[value decimalValue]];
} ....

The "value" is of type NSCFNumber, and I appear to be losing precision in the value mapping from NSCFNumber to NSDecimalNumber.

This is a big deal for me as I'm dealing w/ prices & currency.

Am I missing obvious here? Not seeing how / why this happens and what to do about it.

Thanks!

有帮助吗?

解决方案

It seems to be a known issue of Restkit, and still remains in 0.20

As @blakewatters's comment, server should return floating point number within a string, client still declares NSDecimalNumber for correspondent field, and Restkit will convert more correctly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top