总计CoreData属性 [英] Sum a CoreData attribute

查看:147
本文介绍了总计CoreData属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在iOS编程,所以非常感谢的耐心和提前帮助。
我有一个实体Expense,带有一个名为value的属性(float)。我有一个tableView填充表单coreData与NSFetchedResultsController。
我试图在一个标签(或表头)显示所有我的费用的值的总和,但我不能实现解决方案后阅读苹果文档和谷歌不同的论坛。肯定初学者迷失方向。
感谢任何关于实现这个操作之王的最佳方式的指示,或任何类似的代码显示类似的解决方案。

I´m just starting in iOS programming, so many thanks for the patience and help in advance. I´ve an entity "Expense" with an attribute (float) called "value". I've a tableView populated form coreData with NSFetchedResultsController. I'm trying to show in a label (or table Header) the total Sum of "values" of all my "Expenses", but I can't implement a solution after reading Apple Docs and googling different forums. For sure beginner disorientation. Appreciate any indications about the best way to implement this king of operation, or any kind of code that shows similar solution.

推荐答案

首先。如果要计算和存储货币,则应使用Decimal(nsdecimalnumber的核心数据名称)和NSDecimalNumber。
我使用float实现了你需要的代码。但你应该真的改为NSDecimalNumber。请阅读,了解您应该执行此操作的原因

first of all. You should use Decimal (the core data name for nsdecimalnumber) and NSDecimalNumber if you want to calculate and store a currency. I implemented the code you need with float. But you should really change it to NSDecimalNumber. Read this to know why you should do this

如果要将费用值添加到节头中,很容易。你基本上花费在部分中的所有对象,并总结。

If you want to add the expense value to the section header it's easy. You basically take the expense of all objects in the section and sum it up.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    float expense = 0;
    for (NSManagedObject *object in [sectionInfo objects]) {
        expense += [[object valueForKey:@"expense"] floatValue];
    }
    return [NSString stringWithFormat:@"%@ [Expense: %f]", [sectionInfo name], expense];
}

即使你不使用表中的部分,这将工作。但你应该更改返回的字符串然后。

Even if you don't use sections in your table this will work. But you should change the returned string then.

我想你应该能够理解它。还有一个更通用的方法来做到这一点。我为你写了一点更冗长。它在for循环中使用3行而不是1,但它的作用完全一样。

I think you should be able to understand it. There is also a more general way to do this. I wrote it a little bit more verbose for you. It uses 3 lines instead of 1 in the for-loop but it does exactly the same thing.

float expense = 0;
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
    NSNumber *objectExpenseNumber = [object valueForKey:@"expense"];
    float objectExpense = [objectExpenseNumber floatValue];
    expense = expense + objectExpense;
}
NSLog(@"Expense: %f", expense);

没有太多解释。

编辑:如果使用NSDecimalNumber,这将是代码

edit: this would be the code if you use NSDecimalNumber

NSDecimalNumber *expense = [NSDecimalNumber decimalNumberWithString:@"0"];
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
    NSDecimalNumber *objectExpenseNumber = [object valueForKey:@"expense"];
    expense = [expense decimalNumberByAdding:objectExpenseNumber];
}
NSLog(@"Expense: %@", expense);

这篇关于总计CoreData属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆