I have a large NSArray
containing NSNumbers
like 3, 4, 20, 10, 1, 100,
etc...
How do I get the total sum of all these NSNumbers (3 + 4 + 20 + 10 + 1 + 100 + etc...)
as one total NSInteger
?
Thank you!
You can use this:
NSArray* numbers = //array of numbers
NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"];
NSInteger sum = 0;
for (NSNumber *num in myArray) {
sum += [num intValue];
}
long long sum = ((NSNumber*)[array valueForKeyPath: @"@sum.longLongValue"]).longLongValue;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With