View Full Version : KVC Array Summation

Feb 13, 2009, 09:35 AM
I have been able to use KVC to sum an array with objects that have an [NSNumber] as an instance variable. Can I sum just an array using KVC? My attempts so far have failed.
ie. This code will return an uncaught exception

NSArray *myArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:6], [NSNumber numberWithInt:7], nil];

NSLog(@"%@", [myArray valueForKeyPath:@"@sum"]);

I can easily sum an array of numbers using loops and several lines of code, but I am really trying to get a solid hold on KVC. Any help is appreciated :)

Thanks much,

Feb 13, 2009, 10:29 AM
I know nothing about KVC, but maybe this helps?


Looks like this will work:

NSLog(@"%@", [myArray valueForKeyPath:@"@sum.intValue"]);

MacRumors Guy
Feb 13, 2009, 10:40 AM
edit: delete plz