【发布时间】:2013-07-28 13:40:36
【问题描述】:
我有一个用户输入金额的视图。使用 NSUserDefaults,这个数量被转移到下一个视图,我希望用它进行数学计算。在下一个视图中,我有一个可变数组,用于将数据添加到表中。我想要的是:
视图 2 中的标签 = NSUserDefaults -(所有数组对象加在一起)
我想出了如何将数组对象添加在一起:NSNumber *sum = [transactions valueForKeyPath:@"@sum.self"];。
我试着做数学,但没有运气:self.amountLeftInBudget.text = [[[NSUserDefaults standardUserDefaults] objectForKey:@"AmountToSpend"] - sum];。我收到此错误:指向接口“id”的指针上的算术运算,对于此架构和平台来说,这不是一个恒定的大小。
有人有什么想法吗?谢谢。
编辑:我的代码(使用警报视图)
- (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]];
}
}
两个字符串相减的问题。
【问题讨论】:
标签: ios objective-c math nsuserdefaults nsnumber