Im using core-plot for my graphing component of my iPhone app, and I have been using NSDecimal object a lot.
One of the lines of their code that I have seen is like this:
-(void)plotPoint:(NSDecimal *)plotPoint forPlotAreaViewPoint:(CGPoint)point
{
NSDecimal x;
//do some calculations on x
plotPoint[CPCoordinateX] = x;
}
Where, CPCoordinateX is deinfed as below:
typedef enum _CPCoordinate {
CPCoordinateX = 0, ///< X axis
CPCoordinateY = 1, ///< Y axis
CPCoordinateZ = 2 ///< Z axis
} CPCoordinate;
The line:
plotPoint[CPCoordinateX] = x;
is what I dont understand, how can a NSDecimal be assigned to like this?
In my code, Im trying to call this method, like so:
NSDecimal dec = CPDecimalFromInteger(0);
[plotSpace plotPoint:&dec forPlotAreaViewPoint:point];
NSDecimalNumber *newx = [[NSDecimalNumber alloc] initWithDecimal:dec];
NSDecimal x = dec[CPCoordinateX];
//NSLog(@"converted at: %@", newx);
but Im getting compile errors:
error: subscripted value is neither array nor pointer
Can someone please explain this to me?
plotPointis a pointer and pointers can be indexed like arrays using the subscript operator:You can also use those expressions for assignments:
But you can only use the subscript operator on arrays or pointers:
To actually pass a point
-plotPoint:forPlotArrayViewPoint:you need a C-style array or a dynamic array of 2 or 3NSDecimals (according to what dimensions the method expects), e.g.:On that array you can now also use the subscript operator: