运行核心图教程的问题

时间:2010-06-10 19:48:19

标签: iphone core-plot

我在这里关注核心情节的教程.... http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application

我收到以下代码行的错误

//SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorIntervalLength'
axisSet.xAxis.majorIntervalLength = [NSDecimalNumber decimalNumberWithString:@"5"];

// request for member 'axisLabelOffset' in something not a structure or union
axisSet.xAxis.axisLabelOffset = 3.0f;

//request for member 'bounds' in something not a structure or union
CPScatterPlot *xSquaredPlot = [[[CPScatterPlot alloc] initWithFrame:graph.defaultPlotSpace.bounds] autorelease];

这是我的代码现在我不再得到任何编译器错误但是它崩溃并且没有加载视图请看看你是否可以

  @implementation FirstCorePlotViewController

 (void)viewDidLoad 
{

      [super viewDidLoad];

      graph = [[CPXYGraph alloc] initWithFrame: self.view.bounds];

      CPLayerHostingView *hostingView = (CPLayerHostingView *)self.view;
      hostingView.hostedLayer = graph;
      graph.paddingLeft = 20.0;
      graph.paddingTop = 20.0;
      graph.paddingRight = 20.0;
      graph.paddingBottom = 20.0;

        CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
     plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-6)
                                              length:CPDecimalFromFloat(12)];
     plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-5)
                                               length:CPDecimalFromFloat(30)];

      CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;

          CPLineStyle *lineStyle = [CPLineStyle lineStyle];
      lineStyle.lineColor = [CPColor blackColor];
          lineStyle.lineWidth = 2.0f;

      axisSet.xAxis.majorIntervalLength =CPDecimalFromString(@"5");
      axisSet.xAxis.minorTicksPerInterval = 4;
      axisSet.xAxis.majorTickLineStyle = lineStyle;
      axisSet.xAxis.minorTickLineStyle = lineStyle;
      axisSet.xAxis.axisLineStyle = lineStyle;
      axisSet.xAxis.minorTickLength = 5.0f;
      axisSet.xAxis.majorTickLength = 7.0f;
      axisSet.xAxis.labelOffset = 3.0f;

      axisSet.yAxis.majorIntervalLength = CPDecimalFromString(@"5");
      axisSet.yAxis.minorTicksPerInterval = 4;
      axisSet.yAxis.majorTickLineStyle = lineStyle;
      axisSet.yAxis.minorTickLineStyle = lineStyle;
      axisSet.yAxis.axisLineStyle = lineStyle;
      axisSet.yAxis.minorTickLength = 5.0f;
      axisSet.yAxis.majorTickLength = 7.0f;
      axisSet.yAxis.labelOffset = 3.0f;

     CPScatterPlot *xSquaredPlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];


        xSquaredPlot.identifier = @"X Squared Plot";
    xSquaredPlot.dataLineStyle.lineWidth = 1.0f;
    xSquaredPlot.dataLineStyle.lineColor = [CPColor redColor];
    xSquaredPlot.dataSource = self;
    [graph addPlot:xSquaredPlot];

    CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol];
        greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]];
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
    //xSquaredPlot.defaultPlotSymbol = greenCirclePlotSymbol;  

    CPScatterPlot *xInversePlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];    
    xInversePlot.identifier = @"X Inverse Plot";
    xInversePlot.dataLineStyle.lineWidth = 1.0f;
    xInversePlot.dataLineStyle.lineColor = [CPColor blueColor];
    xInversePlot.dataSource = self;
    [graph addPlot:xInversePlot];
}

-(NSUInteger)numberOfRecords 
 {

        return 51;
 }

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum  
           recordIndex:(NSUInteger)index
 {
        double val = (index/5.0)-5;
    if(fieldEnum == CPScatterPlotFieldX)
    { 

             return [NSNumber numberWithDouble:val]; 
     }
    else
    {
       if(plot.identifier == @"X Squared Plot")
       {
                   return [NSNumber numberWithDouble:val*val]; 
        }
       else
       {
                     return [NSNumber numberWithDouble:1/val];
       }
 }
}

 @end

4 个答案:

答案 0 :(得分:1)

这些错误都不是由#import问题引起的。已知该教程有些过时,Core Plot框架的某些部分已经发生了变化。

  • majorIntervalLength属性需要NSDecimal,而不是NSDecimalNumer。 Core Plot包含几个实用程序函数,可将其他类型转换为NSDecimal,例如CPDecimalFromString和CPDecimalFromDouble。

axisSet.xAxis.majorIntervalLength = CPDecimalFromString(@"5");

  • axisLabelOffset属性已重命名为labelOffset

  • 第三个错误是由两件事引起的。 UIView和CPLayer(所有Core Plot图层的根类)都有-initWithFrame:方法。因为-alloc返回id,所以编译器不知道使用哪个-initWithFrame:有时会猜错。您可以使用类型转换来修复它。另外,绘图空间不是图层;只需获得图表的界限。

CPScatterPlot *xSquaredPlot = [[(CPScatterPlot *)[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease];

答案 1 :(得分:0)

// request for member 'axisLabelOffset' in something not a structure or union

...表示编译器不会将点语法中提供的名称识别为属于该对象。错别字是此错误的常见原因。另一个是没有正确导入点前面的类的标题。

//SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorIntervalLength'

这意味着属性majorIntervalLength不带NSDecimalNumber。

我要说你的所有问题都是由#import语句引起的。你没有在你应该的位置导入标题,并且编译器不理解哪个符号与哪个类相关。

答案 2 :(得分:0)

-numberForPlot:field:recordIndex:方法中存在除零错误。当index == 25时,语句1/val将会爆炸。

答案 3 :(得分:0)

Follow this Tute

这个Ray的教程非常有帮助。