Skip to content

Instantly share code, notes, and snippets.

@saish98
Forked from TonnyXu/kvcDemo.md
Last active August 29, 2015 14:27
Show Gist options
  • Save saish98/796dc85654c568050a6d to your computer and use it in GitHub Desktop.
Save saish98/796dc85654c568050a6d 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