0

I'm trying to use KVC collection to get the average count of arrays in a collection. In other words, my data structure looks like this @[ @[...], @[......], @[..] ] and I am trying to average the count of the internal arrays.

I could do it the old fashion way, but it seems like something KVC collection operators might be able to help with -- unfortunately i can't get it to work.

It seems like this would be the right syntax:

NSNumber *avg = [topLevelArray valueForKeyPath:@"@avg.count"]

Yet this draws an exception -- claiming that the objects contained within the second level of arrays are not KVC compliant for the key 'count'.

Is there a way to run KVC collection operators on second level arrays?

Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90

1 Answers1

2

Simply use the proper @count collection operator [tested]:

NSNumber *avg = [topLevelArray valueForKeyPath:@"@avg.@count"];
Mundi
  • 79,884
  • 17
  • 117
  • 140