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 c开发者_StackOverflow中文版an 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?
plotPoint is a pointer and pointers can be indexed like arrays using the subscript operator:
int array[] = { 1, 2, 3 };
NSLog(@"x=%d, y=%d, z=%d", array[0], array[1], array[2]); 
// prints "x=1, y=2, z=3"
int *pointer = array; // implicit conversion to pointer
NSLog(@"x=%d, y=%d, z=%d", pointer[0], pointer[1], pointer[2]);
// also prints "x=1, y=2, z=3"
You can also use those expressions for assignments:
array[0] = 4;
pointer[1] = 5;
But you can only use the subscript operator on arrays or pointers:
NSDecimal dec = CPDecimalFromInteger(0);
dec[0]; // illegal, dec is a single NSDecimal value, not pointer or array
To actually pass a point -plotPoint:forPlotArrayViewPoint: you need a C-style array or a dynamic array of 2 or 3 NSDecimals (according to what dimensions the method expects), e.g.:
NSDecimal decPoint[] = {
     CPDecimalFromInteger(0),
     CPDecimalFromInteger(0),
     CPDecimalFromInteger(0)
};
[plotSpace plotPoint:decPoint forPlotAreaViewPoint:point];
On that array you can now also use the subscript operator:
NSDecimal x = decPoint[CPCoordinateX];
It's a C array.
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
 加载中,请稍侯......
      
精彩评论