Core Date and NSDecimalNumber

Discussion in 'iOS Programming' started by atkobeau, Jun 20, 2011.

  1. atkobeau macrumors newbie

    Jun 20, 2011
    Hello, I'm learning Objective C for iPhone development.

    Does anyone have any examples of using NSDecimalNumber with Core Data? because my background is java and not c, I'm struggling to take a string from a UITextField, convert it to NSDecimalNumber, put it in Core Data, pull it back out later and put it in a UILabel :confused:
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Which of those steps is the problem?

    The breakdown into those steps seems reasonable, so I'm guessing the problem must be with a specific step.

    What documentation have you looked at, especially for conversions?

    If the problem is with how it's stored in Core Data, have you considered storing it as a string, and only converting to/from NSDecimalNumber when you use it as a number?
  3. atkobeau thread starter macrumors newbie

    Jun 20, 2011
    Well I'm pretty sure that something is being put into the core data but I don't think it's the correct decimal value, when I pull it out again and convert it into a string so I can see it, I'm getting a null value, so I think its the intial conversion from the textfield that is going wrong.
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Post your code. We can't debug descriptions or suppositions ("I'm pretty sure...", "I think...", etc.).

    What have you tried to debug the code?

    If you suspect problems before storing data, what have you done to examine values before storing them?

    The obvious thing is to NSLog() the string, the number, and anything else relevant (like whether the textfield itself is nil). If you have NSLog() output, post it.

    The less obvious thing is to use the debugger, set a breakpoint, and step through so you can see what happens.
  5. atkobeau, Jun 22, 2011
    Last edited by a moderator: Jun 22, 2011

    atkobeau thread starter macrumors newbie

    Jun 20, 2011
    Here is the code where I pull the number from a text field:
    NSString *myNewBudget;
        myNewBudget = myNewBudgetTF.text;   
        BudgetAmount *budgetAmount = (BudgetAmount *)[NSEntityDescription insertNewObjectForEntityForName:@"BudgetAmount" inManagedObjectContext:managedObjectContext];  
        NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
        [currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
        [currencyFormatter setGeneratesDecimalNumbers:TRUE];
        [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
     int currencyScale = [currencyFormatter maximumFractionDigits];
        NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:currencyScale raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE]; 
    NSDecimalNumber *decimalNumber = [currencyFormatter numberFromString:myNewBudget];
    NSDecimalNumber *roundedDecimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
    [budgetAmount setBudgetAmount:roundedDecimalNumber];
    and here is where I pull the decimalNumber out of Core Data
    BudgetAmount *bambam = [budgetAmount objectAtIndex:0];
    NSString *myBudget = [NSString stringWithFormat:@"%@", [bambam BudgetAmount]];
    self.budgetAmountLabel.text = myBudget;
  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    What happens if you add an NSLog() of the variable indicated, at each of the points indicated in red?
    Post the output.

    And you seem to be leaking currencyFormatter.
  7. atkobeau thread starter macrumors newbie

    Jun 20, 2011
    Here you go:

    2011-06-23 06:54:53.277 MyProject[324:207] myNewBudget 123
    2011-06-23 06:54:53.296 MyProject[324:207] decimalNumber (null)
    2011-06-23 06:54:53.296 MyProject[324:207] roundedDecimalNumber (null)

    So it's receiving the value from the text field ok, but then its not converting it.
    Where is currencyFormatter leaking?
  8. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    So it seems. Now it's time to apply basic debugging strategies.

    Start with a careful re-reading of the class reference docs for NSNumberFormatter and NSDecimalNumberHandler. You might see something that you immediately recognize is wrong in your code.

    Next, make a test program with the absolute minimal amount of code. Start with just a plain NSNumberFormatter. It doesn't need a managed object or a text field, just a plain NSString as the input, and an NSDecimalNumber being NSLog'ed as output. It doesn't need NSDecimalNumberHandler at first, either, since the failure plainly occurs without it.

    Run the test and make sure it works. If it doesn't work in minimal form, post the complete code and the output.

    Be sure to try the simplest converter that produces an NSDecimalNumber, which is the NSDecimalNumber method + decimalNumberWithString:.

    If the test program works in minimal form, then start adding to it one statement at a time. Add the statement for setGeneratesDecimalNumbers:TRUE. Run the test again. If the output is still correct, add the next statement. When it doesn't produce the correct output, you've found what causes the problem. Work it out from there, or post again with all your results.

    You create it using alloc, so you own it. Yet you don't release it anywhere in the posted code, so I assume it's not released at all. That's a leak.

    If you do release it later, then it's not a leak. You just didn't post all the relevant code.

Share This Page