I recently read some documentation and some blog entries about Key-Value Coding
and found it extremely useful. I want to utilize it for my case, but I haven't been successful so far.
In my case, I have an NSMutableArray
containing CGPoint
's transformed to NSValue
's. I will be adding many points to this data array and I want to get the minimum/maximum x and y values. For example:
NSMutableArray *data = [[NSMutableArray alloc] init];
[data addObject:[NSValue valueWithCGPoint:CGPointMake(20.0f, 10.0f)]];
[data addObject:[NSValue valueWithCGPoint:CGPointMake(5.0f, -15.0f)]];
[data addObject:[NSValue valueWithCGPoint:CGPointMake(-5.0f, 20.0f)]];
[data addObject:[NSValue valueWithCGPoint:CGPointMake(15.0f, 30.0f)]];
// min X: -5.0, max X: 20.0
// min Y: -15.0, max Y: 30.0
I have two approaches so far. The first one is to store these four values in class instance variables and when a new object is added, compare it with those variables and update them if necessary. The second one involves a for-loop to to find the extremum values, however this approach would be inefficient.
If possible, I would like to use KVC to do this task and I believe that it would be a more general solution than those I have. In the future, I might also need to remove some of the objects from the array and that would make my first approach inapplicable and all I am left with would be the for-loop.
I tried to use some key paths, i.e. @"@max.x"
, @"@max.position.x"
but all I got is an NSUnknownKeyException
.
CGPoint Key Paths If the value of a given property is a CGPoint data type, you can append one of the field names in Table C-3 to the property to get or set that value. For example, to change the x component of a layer’s position property, you could write to the key path position.x.
– CeramicCALayer
, but not with a generalNSValue
, compare https://mcmap.net/q/1008377/-kvc-strange-behavior. – Habitancymax
can be used for example to get the maximum value of an array of NSNumbers. See the answer here. – Ceramic