Reputation: 147
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]];
Upvotes: 2
Views: 449
Reputation: 53000
KVC requires keys to be strings:
A key is a string that identifies a specific property of an object. Typically, a key corresponds to the name of an accessor method or instance variable in the receiving object. Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.
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" of NSArray
.
Upvotes: 1