Search code examples
objective-ccocoakvc

Error Using max/min on Numbers in NSDIctionary


Creating a dictionary with the new literals syntax, thusly:

NSDictionary *valuesByHour = @{
    @9: @2,
    @10: @2,
    @11: @3,
    @12: @3,
    @1: @2,
    @2: @2
};

Then later, I want to find the max value, like this:

return [[self.valuesByHour allValues] valueForKeyPath:@"max.intValue"];

but am getting this error:

[<__NSCFNumber 0x9f6fbe0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key max.

Tried also actually creating NSNumbers the old way, e.g. [NSNumber numberWithInt:2].


Solution

  • You're missing the @ at the beginning of the key path.

    return [[self.valuesByHour allValues] valueForKeyPath:@"@max.intValue"];