When using collection operators (e.g. @min
,@max
) on NSArrays
using Key-Value Coding I get an exception if one of the values in the array is NSNull null
.
I have to remove all NSNull null
occurrences before using valueForKeyPath
to make it work:
NSMutableArray *array = otherArray.mutableCopy;
[array removeObjectIdenticalTo:[NSNull null]];
NSNumber *minValue = [array valueForKeyPath:@"@min.self"];
NSNumber *maxValue = [array valueForKeyPath:@"@max.self"];
However, according to Apple's Documentation my code should work without removing NSNull null
:
If the value of the right side of the key path is nil, it is ignored.
How can I use the keyPath statements to get the minimum and maximum values of the array without getting an runtime exception and without having to remove NSNull null
before?