Stuck on KVCs in Obj-C again.
I am wanting to use KVC to find the minimum value across multiple keys.
Consider the following array:
NSArray *data = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:2.0], @"a", [NSNumber numberWithFloat:5.0], @"b", [NSNumber numberWithFloat:4.0], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:3.0], @"a", [NSNumber numberWithFloat:1.0], @"b", [NSNumber numberWithFloat:1.5], @"c", nil],
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:9.0], @"a", [NSNumber numberWithFloat:7.0], @"b", [NSNumber numberWithFloat:6.0], @"c", nil],
nil];
I can find the minimum value for 'a', 'b', or 'c' easily with:
float minKeyA = [[data valueForKeyPath:@"@min.a"] floatValue]; // value: 2.0
float minKeyB = [[data valueForKeyPath:@"@min.b"] floatValue]; // value: 1.0
float minKeyC = [[data valueForKeyPath:@"@min.c"] floatValue]; // value: 1.5
What I am wanting to achieve is to take a NSSet or NSArray of keys and find the minimum value across the pool of those keys.
NSSet *keySet1 = [NSSet setWithObjects:@"a", @"b", nil];
// use to find absolute minimum value across keys 'a' and 'b' --> desired value 1.0
NSSet *keySet2 = [NSSet setWithObjects:@"a", @"c", nil];
// use to find absolute minimum value across keys 'a' and 'c' --> desired value 1.5
NSSet *keySet3 = [NSSet setWithObjects:@"a", @"b", @"c", nil];
// use to find absolute minimum value across keys 'a', 'b', and 'c' --> desired value 1.0
Appreciate any pointers :)
A naive solution would be to first find the minimum value for each key and then find the minimum among those minimum values in a second step.
NSMutableSet *localMinima = [NSMutableSet setWithCapacity:[keySet1 count]];
for (NSString *key in keySet1) {
NSString *keyPath = [NSString stringWithFormat:@"@min.%@", key];
NSNumber *localMin = [data valueForKeyPath:keyPath];
[localMinima addObject:localMin];
}
NSNumber *globalMin = [localMinima valueForKeyPath:@"@min.self"];