How get the total sum of NSNumber's from a NSArray?

泄露秘密 提交于 2019-12-03 09:36:21
NSInteger sum = 0;
for (NSNumber *num in myArray) {
  sum += [num intValue];
}

You can use this:

NSArray* numbers = //array of numbers
NSNumber* sum = [numbers valueForKeyPath: @"@sum.self"];
fabrice truillot de chambrier
long long sum = ((NSNumber*)[array valueForKeyPath: @"@sum.longLongValue"]).longLongValue;

Iterate through the array

int count = [array count];
NSInteger sum = 0;
for (int i = 0; i < count; i++) {
    sum += [[array objectAtIndex:i] integerValue];
}
[[numbersArray valueForKeyPath:@"@sum.self"] integerValue]
int total = 0;
for (NSNumber *number in array)
{
  total += [number intValue];
}

may this will help you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!