When save NSDecimalNumber in EditViewController, in DisplayTableViewController value appear NaN

Please need advice to solve this issue. Thanks

My code:

- (IBAction)saveChanges:(id)sender {
    editGift.nameOfGift = nameTextField.text;
    editGift.priceOfGift = [NSDecimalNumber decimalNumberWithString:priceTextField.text];

    AppDelegate *myApp = (AppDelegate *) [[UIApplication sharedApplication]delegate];
    [myApp saveContext];
    [self.delegate editGiftViewControllerDidSave:self];
}

Please watch video explanation

Input value:

currencyFormatter = [[NSNumberFormatter alloc]init]; 

[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

 personGift.priceOfGift = [NSDecimalNumber decimalNumberWithString:priceTextField.text];
有帮助吗?

解决方案

Your code is unclear about when it applies the currency formatting, but given your video it is very likely that the value of the text field is "$6,000". That is not a number (thus "NaN").

其他提示

NSDecimalNumber documentation says:

Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number.

You have to verify that the string in priceTextField.text contains only valid characters - and in valid order. One thing to check especially is whether you have decimal point or decimal comma, depending on user locale. Therefore you might want to consider using:

+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericString
locale:(NSDictionary *)locale
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top