I've an entity "Expense" with an attribute (float) called "value". I've a table view populated from 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.

- 11,118
- 5
- 63
- 81

- 45
- 2
- 5
1 Answers
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.
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);
Not really much to explain.
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);

- 1
- 1

- 89,811
- 20
- 225
- 247
-
Thanks for taking some time "fluchtpunkt", and pointing a way to work with currencies. Good advices are always welcome and taken. My main issue was the way to calculate the @Sum of the "value" for all the Expenses a user can insert in a TableView and stored as an Entity "Expense" with the attribute "value" or "amount", more correctly. I didn´t find a way to calculate the total sum of an attribute with CoreData. Then, I would show that "Total Amount" somewhere. Thanks – Diogo Jan 23 '11 at 19:07
-
Thanks fluchtpunkt, I've been working on your code, trying to understand it and not just "paste it". It's working as wished. – Diogo Jan 24 '11 at 15:44