尝试使用核心图进行编译时获取 EXC_BAD_ACCESS

发布于 2024-11-28 13:04:05 字数 1247 浏览 0 评论 0原文

作为 Objective C 和 xcode 的初学者,我正在尝试编写一个教程程序,可以在那里找到: 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,它是一个自动释放,这就是我收到错误的原因,但我理解的是,在我的 .h 中的属性中进行保留,并在我的 .m 中进行综合。但这并没有解决问题。

因此,我们将非常感谢任何帮助,如果我错过了答案,我很抱歉,尽管它已经在论坛上。

问候,克拉夫蒂。

As a beginner in objective C and xcode, I am trying to do a tutorial program that can be found there: http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application

I did some changes so it would finally compile, so here's my .h (please forgive the silly name of my application):

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

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

}

@end

And here's my .m (at least the important part):

#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];

And I get the EXC_BAD_ACCESS at the first non-commentary line of the three last lines, right after the application started running.

Although I am a beginner, I spent a lot of time looking into this and could not find the solution on internet. It seems like I'm trying to access xSquaredtPlot which is an autorelease and that is why I get the error, but what I understood is that doing a retain in a property in my .h, and a synthesize in my .m. BUt that didn't fix the problem.

So any help will be gladly appreciated and I'm sorry if I missed the answer although it's already on the forums.

Regards, Crafti.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2024-12-05 13:04:05

问题是过时的教程。它已经推出两年多了,从那时起核心情节发生了很多变化。关键的一步是除了 -ObjC 之外,您还需要添加一个额外的链接器标志。您还需要添加 -all_load

Screenshot of linker flags

我浏览了教程并更新了它可以与当前版本的 core-plot 一起使用。请注意,视图的类需要设置为 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

The problem is the outdated tutorial. It's over two years old and a lot has changed in core-plot since then. The crucial step is you need to add an additional linker flag besides -ObjC. You also need to add -all_load:

Screenshot of linker flags

I went through the tutorial and updated it to work with the current version of core-plot. Note that the view's class needs to be set to CPTGraphHostingView not CPLayerHostingView. Here is my working version:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文