【问题标题】:Use NSNumber and NSUserDefaults to do a simple computation使用 NSNumber 和 NSUserDefaults 做一个简单的计算
【发布时间】: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


    【解决方案1】:

    当你在 UILabel 上设置文本时,你必须给它一个 NSString。因此,您标记为问题的行将不起作用...

    // Set the amountLeftInBudget label THE PROBLEM! the subtracting does not work
    [self.amountLeftInBudget setText:[amountToSpend decimalNumberBySubtracting:sum]];
    

    ...因为你给 UILabel 一个 NSDecimalNumber。先把 NSDecimalNumber 转成字符串:

    [self.amountLeftInBudget setText:[[amountToSpend decimalNumberBySubtracting:sum] stringValue]];
    

    【讨论】:

    • 我尝试了您的解决方案,但应用程序崩溃了。问题可能是 NSUserDefaults。你怎么看?
    • 我的回答假设 amountToSpend 是您的代码从前一行中的 NSUserDefaults 检索到的有效 NSDecimalNumber 对象。您可以通过设置断点并使用调试器检查 amountToSpend 并确认它是 NSDecimalNumber 来检查此假设。我认为您可能没有正确地将 NSDecimalNumber 写入 NSUserDefaults 开始。顺便说一句,像这样使用 NSUserDefaults 传递变量有点不寻常;您可能会发现将 amountToSpend 设为实例变量会更容易。
    【解决方案2】:

    检查你的表情,在- 的左侧你有:

    [[NSUserDefaults standardUserDefaults] objectForKey:@"AmountToSpend"]
    

    那个表达式的是什么?它是对对象的一些引用。现在 rhs 是:

    sum
    

    并且该表达式的是对NSNumber的一些引用。那么value是什么:

    <some reference to an object> - <some reference to an `NSNumber`>
    

    您要求两个引用之间的差异,因此错误:Arithmetic on pointer to interface 'id',这对于该架构和平台来说不是一个恒定的大小。

    您需要获取您的对象所代表的数值,对它们进行算术运算,然后将数值结果转换为字符串表示形式,以将其分配给您的 text 属性。

    NSNumber有多种获取对象所代表数值的方法;例如doubleValueintValue等;您需要根据自己的需要选择合适的。

    HTH。

    【讨论】:

    • 感谢您的回答。这很有道理。我如何获得用户默认值?
    • @ShaanSingh CRD 在最后一段中回答了这个问题。如果您不知道如何从 NSNumber 获取数值,我建议您先退后一步,回顾一下该语言的一些基础知识。
    • 那些也可以用于 NSUserDefaults?
    • @ShaanSingh - 如果您知道从NSUserDefaults 返回的对象是NSNumber,那么您可以对其使用NSNumber 方法。但是您不需要,在NSUserDefaults 的文档中查找“获取默认值” - 您可以使用诸如stringForKey: 等方法直接获取类型选择。
    • @ShaanSingh - 现在您还没有解析作为数字的字符串,字符串@"9" 没有int 值9 而不是字符串@987654339 @ 确实 - 它们都是 9 的字符表示。将 objectForKey: 返回的任意对象分配给 NSDecimalNumber * 变量不会转换对象 - 正如您在您的案例中发现的那样,返回并简单地存储了 NSString 引用。查找方法doubleValue(在NSString)和doubleForKey:(在NSUserDefaults)。你似乎有一些基本的误解;听取 bbum 的建议并审查基本面。
    猜你喜欢
    • 2023-03-17
    • 2020-12-18
    • 2020-06-06
    • 1970-01-01
    • 2012-08-16
    • 2014-08-02
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    相关资源
    最近更新 更多