Search code examples
ioskey-value-coding

nsarray using Key-Value Collection Operators


I'm just learning about Collection Operators and my first example isn't working. How can I fix this?

NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], 
                [NSNumber numberWithInt:10], 
                [NSNumber numberWithInt:100], 
                [NSNumber numberWithInt:1000], 
                nil];
NSLog(@"%@", [arr valueForKeyPath:@"@min"]);

I crash out with:

this class is not key value coding-compliant for the key min


Solution

  • From your link:

    All the collection operators, with the exception of @count, require a key path to the right of the collection operator.

    For this case, use

    [arr valueForKeyPath:@"@min.intValue"];