Skip to content

Instantly share code, notes, and snippets.

@TonnyXu
Created August 24, 2012 08:56
Show Gist options
  • Save TonnyXu/3447781 to your computer and use it in GitHub Desktop.
Save TonnyXu/3447781 to your computer and use it in GitHub Desktop.
using KVC on collection objects like NSArray and NSDictionary.

KVCを使えば、このようなコードが自動的に値を計算してくれる。

KVC with special keyPath is really powerful for collection objects like NSArray and NSDictionary and NSSet

AppleのDocを見る

@max, @min, @sum, @avg

Sample 1 : One of the element in the array does not contain height property

  NSArray *testDicts = @[ @{ @"height" : @12 }, @{ @"height" : @13 }, @{ @"height" : @13 }, @{ @"height" : @13 }, @{ @"height" : @14 }, @{ @"height" : @15 }, @{ @"height" : @16 }, @{ @"id" : @17 } ];
  NSNumber *sum = [testDicts valueForKeyPath:@"@sum.height"];
  NSNumber *max = [testDicts valueForKeyPath:@"@max.height"];
  NSNumber *min = [testDicts valueForKeyPath:@"@min.height"];
  NSNumber *avg = [testDicts valueForKeyPath:@"@avg.height"];
  NSNumber *count = [testDicts valueForKeyPath:@"@count.height"];
  NSArray *distinctUnionOfObjects = [testDicts valueForKeyPath:@"@distinctUnionOfObjects.height"];
  NSArray *unionOfObjects = [testDicts valueForKeyPath:@"@unionOfObjects.height"];
  NSLog(@"sum = %@ | max = %@ | min = %@ | avg = %@ | count = %@", sum, max, min, avg, count);
  NSLog(@"\ndis: %@\nunion:%@", distinctUnionOfObjects, unionOfObjects);
2012-08-24 17:53:03.788 [GGTV] sum = 96 | max = 16 | min = 12 | avg = 12 | count = 8
2012-08-24 17:53:03.788 [GGTV] 
dis: (
    15,
    13,
    16,
    14,
    12
)
union:(
    12,
    13,
    13,
    13,
    14,
    15,
    16
)

Sample 2 : All the elements in Array contain property height

  NSArray *testDicts = @[ @{ @"height" : @12 }, @{ @"height" : @13 }, @{ @"height" : @13 }, @{ @"height" : @13 }, @{ @"height" : @14 }, @{ @"height" : @15 }, @{ @"height" : @16 }, @{ @"height" : @17 } ];
  NSNumber *sum = [testDicts valueForKeyPath:@"@sum.height"];
  NSNumber *max = [testDicts valueForKeyPath:@"@max.height"];
  NSNumber *min = [testDicts valueForKeyPath:@"@min.height"];
  NSNumber *avg = [testDicts valueForKeyPath:@"@avg.height"];
  NSNumber *count = [testDicts valueForKeyPath:@"@count.height"];
  NSArray *distinctUnionOfObjects = [testDicts valueForKeyPath:@"@distinctUnionOfObjects.height"];
  NSArray *unionOfObjects = [testDicts valueForKeyPath:@"@unionOfObjects.height"];
  NSLog(@"sum = %@ | max = %@ | min = %@ | avg = %@ | count = %@", sum, max, min, avg, count);
  NSLog(@"\ndis: %@\nunion:%@", distinctUnionOfObjects, unionOfObjects);
2012-08-24 17:54:35.986 [GGTV] sum = 113 | max = 17 | min = 12 | avg = 14.125 | count = 8
2012-08-24 17:54:35.986 [GGTV] 
dis: (
    17,
    15,
    13,
    16,
    14,
    12
)
union:(
    12,
    13,
    13,
    13,
    14,
    15,
    16,
    17
)

Conclusion

  1. Sample 1 might be a bug, but we need to live with it now.
  2. It's really convenient.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment