Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

atkobeau

macrumors newbie
Original poster
Jun 20, 2011
4
0
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:
 

chown33

Moderator
Staff member
Aug 9, 2009
10,745
8,418
A sea of green
... 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:

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?
 

atkobeau

macrumors newbie
Original poster
Jun 20, 2011
4
0
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.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,745
8,418
A sea of green
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.
 

atkobeau

macrumors newbie
Original poster
Jun 20, 2011
4
0
Here is the code where I pull the number from a text field:
Code:
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
Code:
BudgetAmount *bambam = [budgetAmount objectAtIndex:0];
        
NSString *myBudget = [NSString stringWithFormat:@"%@", [bambam BudgetAmount]];

self.budgetAmountLabel.text = myBudget;
 
Last edited by a moderator:

chown33

Moderator
Staff member
Aug 9, 2009
10,745
8,418
A sea of green
Code:
NSString *myNewBudget;
    
    myNewBudget = myNewBudgetTF.text;   
    [COLOR="Red"]#myNewBudget#[/COLOR]
    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];
[COLOR="Red"]#decimalNumber#[/COLOR]
NSDecimalNumber *roundedDecimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
[COLOR="Red"]#roundedDecimalNumber#[/COLOR]
[budgetAmount setBudgetAmount:roundedDecimalNumber];
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.
 

atkobeau

macrumors newbie
Original poster
Jun 20, 2011
4
0
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?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,745
8,418
A sea of green
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.
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.


Where is currencyFormatter leaking?
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.
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/

If you do release it later, then it's not a leak. You just didn't post all the relevant code.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.