Reputation:
I have a view where a user enters an amount. Using NSUserDefaults, this amount is transferred to the next view, where I wish to do a mathematical computation with it. In this next view, I have a mutable array that is used for adding data to a table. What I want is:
Label in view 2 = NSUserDefaults - (all array objects added together)
I figured out how to add the array objects together: NSNumber *sum = [transactions valueForKeyPath:@"@sum.self"];
.
I tried to do the math, but no luck: self.amountLeftInBudget.text = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AmountToSpend"] - sum];
. I got this error: Arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform.
Anyone have any ideas? Thanks.
Edit: My code (using an alert view)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Only do the following actions if the user hit the done button
if (buttonIndex == 1)
{
NSString *amountSpentTextField = [alertView textFieldAtIndex:0].text;
if (!transactions)
{
transactions = [[NSMutableArray alloc]init];
}
[transactions insertObject:amountSpentTextField atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.mytableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//Add all the objects in the transactions array
NSDecimalNumber *sum = [transactions valueForKeyPath:@"@sum.self"];
//Get the user defaults from the amountToSpend field
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDecimalNumber *amountToSpend = [defaults objectForKey:@"AmountToSpend"];
// Set the amountLeftInBudget label THE PROBLEM! the subtracting does not work
[self.amountLeftInBudget setText:[amountToSpend decimalNumberBySubtracting:sum]];
//Set the amountSpent label
[self.amountSpent setText:[sum stringValue]];
}
}
The problem in trying to subtract the two strings.
Upvotes: 1
Views: 549
Reputation: 549
When you set the text on a UILabel you have to give it an NSString. So the line that you flag as a problem won't work...
// Set the amountLeftInBudget label THE PROBLEM! the subtracting does not work
[self.amountLeftInBudget setText:[amountToSpend decimalNumberBySubtracting:sum]];
...because you're giving UILabel an NSDecimalNumber. Convert the NSDecimalNumber into a string first:
[self.amountLeftInBudget setText:[[amountToSpend decimalNumberBySubtracting:sum] stringValue]];
Upvotes: 0
Reputation: 53010
Examine your expression, on the left-hand-side of -
you have:
[[NSUserDefaults standardUserDefaults] objectForKey:@"AmountToSpend"]
What is the value of that expression? It is some reference to an object. Now the rhs is:
sum
and the value of that expression is some reference to an NSNumber
. So what is the value
of:
<some reference to an object> - <some reference to an `NSNumber`>
You are asking for the difference between two references, hence the error: Arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform.
You need to obtain the numeric value your objects represent, do your arithmetic on those, and then convert the numeric result to a string representation to assign it to your text
property.
NSNumber
has various methods to obtain the numeric value the object represents; e.g. doubleValue
, intValue
, etc.; and you need to select the appropriate one depending on your needs.
HTH.
Upvotes: 1