KVC with special keyPath
is really powerful for collection objects like NSArray
and NSDictionary
and NSSet
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
)
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
)
- Sample 1 might be a bug, but we need to live with it now.
- It's really convenient.