There are some special key value coding operators that work on arrays and sets, and one of them is @sum
. If you fetch all the objects that you want to sum into a set called costs
, and if the attribute that you want to sum for each object is value
, you can then use the @sum operator like this:
float theSum = [costs valueForKeyPath:@"@sum.value"];