开发者

Average NSDictionaries in an NSArray by date

开发者 https://www.devze.com 2023-04-05 02:44 出处:网络
I\'m trying to output to a graph. I have an array of N开发者_如何学编程SDictionaries. Each of these dictionaries have an NSDate (objectForKey:@\"date\") and a value (objectForKey:@\"value\").

I'm trying to output to a graph.

I have an array of N开发者_如何学编程SDictionaries. Each of these dictionaries have an NSDate (objectForKey:@"date") and a value (objectForKey:@"value").

What I want to do is average all the results per day and then order it by date, putting in missing days (the results are all from the last 7 days).

Is there any easy way to do this at all?

Thanks


Not tested, but something like this?

NSArray* ary = ...;

__block double sum = 0.0;

[ary enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  sum += [[obj objectForKey:@"value"] doubleValue];
}];

double average = sum / [ary count];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号