快速方法来总结NSSet中的所有对象的属性? [英] Quick way to sum a property of all objects within an NSSet?
问题描述
我想有一种方法可以快速请求 NSSet
来轮询其成员,并返回一个 NSInteger
属性在每个对象,但我可能会很困惑这与Mac OS X的东西。这是否存在于Cococa Touch?
I thought there was a way to quickly ask a NSSet
to poll its members and return a sum of say an NSInteger
property in each of its objects, but I may very well be confusing this with the Mac OS X side of things. Does this exist in Cococa Touch?
我能找到的最接近的是 objectEnumerator
,我想我可以步枪通过每个对象和增量我自己的变量。
The closest thing I can find is objectEnumerator
, whereby I suppose I could rifle through each object and increment my own variable. Does the better way exist?
推荐答案
如果你想找到给定属性的总和( theSet
)的每个成员的每个成员都可以执行以下操作:< /> theIntegerPropertyToSum p>
If you're trying to find the sum of a given property (theIntegerPropertyToSum
) for each member of an array/set-derived class that's KVC-compliant (theSet
), you can do the following:
NSNumber* theSum = [theSet valueForKeyPath:@"@sum.theIntegerPropertyToSum"];
这篇关于快速方法来总结NSSet中的所有对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!