I have an NSArray which contains NSDictionary objects with keys that are NSNumber objects. I would like to calculate the max value using valueForKeyPath. If I were using strings in the following example, I would use valueForKeyPath:@”@max.OHLCClose”. How do I do the same with NSNumber objects as keys?
typedef enum _OHLCField {
OHLCOpen,
OHLCClose
} OHLCField;
NSMutableArray *newData = [NSMutableArray array];
newData addObject: [NSDictionary dictionaryWithObjectsAndKeys:
[NSDecimalNumber numberWithDouble:fOpen], [NSNumber numberWithInt:OHLCOpen],
[NSDecimalNumber numberWithDouble:fClose], [NSNumber numberWithInt:OHLCClose]];
KVC requires keys to be strings:
So the answer is unfortunately you can’t do this with
valueForKeyPath:.If you need to use
NSNumber‘s as your keys you will have to code the algorithm yourself – just iterate over the array and find the maximum value associated with your key. You could wrap the algorithm in a category so it becomes “part” ofNSArray.