开发者

Average NSDictionaries in an NSArray by date

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜