iOS 数组求和的一种方法

利用dispatch_apply 数组求和

    NSArray * data = [NSArray arrayWithObjects:@(1),@(2),@(3),@(4),@(5),@(6),nil] ;
    __block NSInteger sum = 0;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(data.count, queue, ^(size_t i) {
        sum += [data[i] integerValue];
    });
    NSLog(@"sum = %ld",(long)sum);

其他方式:

    NSNumber *sum = [data valueForKeyPath:@"@sum.floatValue"];
    NSNumber *avg = [data valueForKeyPath:@"@avg.floatValue"];
    NSNumber *max = [data valueForKeyPath:@"@max.floatValue"];
    NSNumber *min = [data valueForKeyPath:@"@min.floatValue"];
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页