本文介绍了iPhone:Core Data,如何从db获取值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型笔,它有这样的属性:makerName和计数。如何获得所有的计数的总和? Core Data能为我计算吗?感谢...

I have a model 'Pens' and it has such properties: "makerName" and "Count". How can I get sum of all 'Count' ? Can Core Data count that for me ? Thanks...

推荐答案

有两种方法可以解决这个问题:

first - 到NSSet或NSArray并使用@sum运算符:

There are two ways to solve this problem:
first - get your data to NSSet or NSArray and use @sum operator:

//assume that `pens` are NSArray of Pen
NSNumber *countSum=[pens valueForKeyPath:"@sum.count"];

其次是使用特定撷取和添加的与一个总和。这种方式更难,但对于更大的数据库

second is using specific fetch for specific value with added NSExpressionDescription with a sum. This way is harder but better for larger db's

这篇关于iPhone:Core Data,如何从db获取值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:42