Quick way to sum a property of all objects within an NSSet?
Asked Answered
N

2

7

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?

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?

Nuncio answered 10/7, 2009 at 1:39 Comment(1)
Are you talking about NSArrayController arrangedObjects.@sum. ... stuff? I'm not sure how to connect it with NSSet and how to get count of specific class members ... I'll hv to read up on that.Murrumbidgee
D
15

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"];
Dissect answered 10/7, 2009 at 3:2 Comment(4)
This is great! Where, btw, is it documented? I can't seem to find anything in/around the definition of "valueForKeyPath:" in the KVC Protocol Reference...Nuncio
they are called key value coding collection operators and the document is hard to find... developer.apple.com/library/ios/#documentation/cocoa/conceptual/…Kast
This code is not maintenance friendly and will confuse people down the line.Olsen
This is a cool feature of Objective C. One cannot do this in C++.Woolgrower
O
-3

Why not use plain objective C (2)?

NSInteger theSum = 0;
for (id obj in theSetOrArray)
    theSum += obj.theIntegerPropertyToSum;

Disadvantage: If you like to count lines, then this looks longer. Are there other disadvantages - I wonder what happens with the KVC method with objects that don't have the required 'theIntegerPropertyToSum' property?

Advantage: I would bet that this debugs and performance tests easier. Plus when someone else reads your code in a year or two they will know what is going on here - whether they have ever seen a line of objective C or not, this looks like what is actually happening.

Olsen answered 10/7, 2009 at 6:17 Comment(4)
Your answer implies that you've not used or read the documentation on KVC -- you might want to read about it before saying it shouldn't be used. In your "plain objective C (2)" example, what happens when theIntegerPropertyToSum is undefined, or one value of theIntegerPropertyToSum is -1 and another is 1.2? What if it's nil?Dissect
I use KVC all the time - to let the UI communicate with the code. The disadvantage of [theSet valueForKeyPath:@"@sum.theIntegerPropertyToSum"] is that is a language construct that does not show at a glance what is going on. Any javascript C, C# programmer seeing the valueForKeyPath statement will not understand it - and be sucked into a time consuming lesson in order to figure out what one line of code does. Never use language specific constructs unless you need them. Reminds me of STL C++ code, which is unreadable to all but STL experts.Olsen
Sounds like you use KVO all the time, not KVC.Dissect
"Never use language specific constructs unless you need them."?! That's the dumbest thing I've heard all week. Sure, why bother learning anything? (By the way, a C programmer wouldn't know what your fast enumeration for loop there is, either.) (Also, KVC is a framework feature, not language -- notice the argument is just a plain old string.)Liard

© 2022 - 2024 — McMap. All rights reserved.