2011-08-09 123 views
0

作爲目標C和Xcode的初學者編譯時獲得EXC_BAD_ACCESS,我嘗試這樣做,可以在那裏發現了一個教程程序:http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application試圖與核心情節

我做了一些變化,因此最終會編譯,所以這裏是我的.h(請原諒我的應用程序的傻名字):

#import <UIKit/UIKit.h> 
#import "CorePlot-CocoaTouch.h" 

@interface yutyyutViewController : UIViewController <CPTPlotDataSource> 
{ 
    CPTXYGraph *graph; 

} 

@end 

而且這裏是我的.m(至少是重要的組成部分):

#import "yutyyutViewController.h" 

@implementation yutyyutViewController 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle]; 

xSquaredtPlot.identifier = @"X Squared Plot"; 

dataLineStyle.lineWidth = 1.0f; 
dataLineStyle.lineColor = [CPTColor redColor]; 

xSquaredtPlot.dataLineStyle = dataLineStyle; 
xSquaredtPlot.dataSource = self; 

[graph addPlot:xSquaredtPlot]; 

我也得到ŧ他在應用程序開始運行之後的最後三行中的第一個非註釋行EXC_BAD_ACCESS。

雖然我是初學者,但我花了很多時間考慮這一點,但無法在互聯網上找到解決方案。這似乎是我試圖訪問xSquaredtPlot這是一個autorelease,這就是爲什麼我得到的錯誤,但我的理解是,在我的.h做一個屬性保留,並在我的.m合成。 BUT沒有解決問題。

所以,任何幫助將很樂意欣賞,我很抱歉,如果我錯過了答案,但它已經在論壇上。

問候,Crafti。

+1

xSquaredtPlot聲明在哪裏? – thomashw

+0

如果您有崩潰,請發回追蹤。 – bbum

+0

由於某種原因,並非我在郵件中寫的所有代碼都發布了......明天我會發布宣言。我沒有崩潰,它只是停止工作,但在日誌中沒有錯誤消息。 – 2011-08-09 22:45:38

回答

2

問題是過時的教程。它已經有兩年多了,自那時起核心情節發生了很大變化。關鍵的一步是除了-ObjC之外,您還需要添加額外的鏈接器標誌。您還需要添加-all_load

Screenshot of linker flags

我通過教程去,並更新其與核心情節的當前版本的工作。請注意,視圖的類需要設置爲CPTGraphHostingView而不是CPLayerHostingView。這是我的工作版本:

#import "CorePlotTestViewController.h" 

@implementation CorePlotTestViewController 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

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

    CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view; 
    hostingView.hostedGraph = graph; 
    graph.paddingLeft = 20.0; 
    graph.paddingTop = 20.0; 
    graph.paddingRight = 20.0; 
    graph.paddingBottom = 20.0; 

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-6) 
                length:CPTDecimalFromFloat(12)]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-5) 
                length:CPTDecimalFromFloat(30)]; 

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; 
    lineStyle.lineColor = [CPTColor blackColor]; 
    lineStyle.lineWidth = 2.0f; 

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

    axisSet.xAxis.majorIntervalLength = CPTDecimalFromString(@"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 = CPTDecimalFromString(@"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; 

    CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] initWithFrame:graph.bounds] autorelease]; 
    xSquaredPlot.identifier = @"X Squared Plot"; 
    CPTMutableLineStyle *plotLineStyle = [[xSquaredPlot.dataLineStyle mutableCopy] autorelease]; 
    plotLineStyle.lineWidth = 1.0f; 
    plotLineStyle.lineColor = [CPTColor redColor]; 
    xSquaredPlot.dataLineStyle = plotLineStyle; 
    xSquaredPlot.dataSource = self; 
    [graph addPlot:xSquaredPlot]; 

    CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; 
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); 
    xSquaredPlot.plotSymbol = greenCirclePlotSymbol; 

    CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc] 
            initWithFrame:graph.bounds] autorelease]; 
    xInversePlot.identifier = @"X Inverse Plot"; 
    plotLineStyle = [[xInversePlot.dataLineStyle mutableCopy] autorelease]; 
    plotLineStyle.lineWidth = 1.0f; 
    plotLineStyle.lineColor = [CPTColor blueColor]; 
    xInversePlot.dataLineStyle = plotLineStyle; 
    xInversePlot.dataSource = self; 
    [graph addPlot:xInversePlot]; 
} 

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { 
    return 51; 
} 

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

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

@end 
+0

你好SSTeve,非常感謝你的回答,它解決了我所有的問題。 我可以問你,你是如何提出你所做的改變的?核心情節還有什麼變化嗎?或者你只是瀏覽頭文件並查看新方法? – Crafti

+0

我看到他們將課程前綴從'CP'改爲'CPT',所以我首先進行了修改。通過源文件查看,我看到CPTLayerHostingView位於文件夾「MacOnly」中,而「CPTGraphHostingView」位於「iPhoneOnly」中,因此將視圖的類更改爲「CPTGraphHostingView」只是一個有教養的猜測。 (續) – SSteve

+0

當我遇到錯誤設置lineStyle屬性時,我搜索了「lineStyle」的示例程序,發現它們已將它重構爲單獨的CPTLineStyle和CPTMutableLineStyle類。我在示例程序中複製了該方法來更改圖的'lineStyle's。任何其他更改都是通過查看頭文件來進行的。最後,我在_README中爲Static Library Install_找到了'-all_load'標誌。 – SSteve