我的代码中的EXC_BAD_ACCESS

时间:2014-04-17 12:02:55

标签: ios objective-c nsdictionary objective-c-blocks exc-bad-access

这部分代码可以访问不好吗? 我正在尝试绘制图形。在我的功能中我有BAD_ACCESS.I找不到有什么问题。 - (void)drawPlot:(SHPlot *)plot {

NSDictionary *theme = plot.plotThemeAttributes;

CAShapeLayer *backgroundLayer = [CAShapeLayer layer];
backgroundLayer.frame = self.bounds;
backgroundLayer.fillColor = ((UIColor *)theme[kPlotFillColorKey]).CGColor;
backgroundLayer.backgroundColor = [UIColor clearColor].CGColor;
[backgroundLayer setStrokeColor:[UIColor clearColor].CGColor];
[backgroundLayer setLineWidth:((NSNumber *)theme[kPlotStrokeWidthKey]).intValue];

CGMutablePathRef backgroundPath = CGPathCreateMutable();

CAShapeLayer *circleLayer = [CAShapeLayer layer];


UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 75, 75)];
circleLayer.path = circle.CGPath;
circleLayer.strokeColor = [UIColor orangeColor].CGColor;
circleLayer.fillColor = [UIColor whiteColor].CGColor;
circleLayer.shadowColor =[UIColor blackColor].CGColor;
circleLayer.lineWidth = 3.0;

[self.layer addSublayer:circleLayer];

CGMutablePathRef circlePath = CGPathCreateMutable();

CAShapeLayer *graphLayer = [CAShapeLayer layer];
graphLayer.frame = self.bounds;
graphLayer.fillColor = [UIColor clearColor].CGColor;
graphLayer.backgroundColor = [UIColor clearColor].CGColor;
[graphLayer setStrokeColor:((UIColor *)theme[kPlotStrokeColorKey]).CGColor];
[graphLayer setLineWidth:((NSNumber *)theme[kPlotStrokeWidthKey]).intValue];

CGMutablePathRef graphPath = CGPathCreateMutable();

double yRange = [_yAxisRange doubleValue];
double yIntervalValue = yRange / INTERVAL_COUNT;

[plot.plottingValues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    NSDictionary *dic = (NSDictionary *)obj;

    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        int xIndex = [self getIndexForValue:key forPlot:plot];
        double height = self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE;
        double y = height - ((height / ([_yAxisRange doubleValue] + yIntervalValue)) * [obj doubleValue]);
        (plot.xPoints[xIndex]).x = ceil((plot.xPoints[xIndex]).x);
        (plot.xPoints[xIndex]).y = ceil(y);
    }];
}];

CGPathMoveToPoint(graphPath, NULL, _leftMarginToLeave, plot.xPoints[0].y);
CGPathMoveToPoint(backgroundPath, NULL, _leftMarginToLeave, plot.xPoints[0].y);

int count = _xAxisValues.count;
for(int i=0; i< count; i++){
    CGPoint point = plot.xPoints[i];
    CGPathAddLineToPoint(graphPath, NULL, point.x, point.y);
    CGPathAddLineToPoint(backgroundPath, NULL, point.x, point.y);
    CGPathAddEllipseInRect(circlePath, NULL, CGRectMake(point.x - 5, point.y - 5, 10, 10));
}
CGPathAddLineToPoint(backgroundPath, NULL, _leftMarginToLeave + PLOT_WIDTH, self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE);
CGPathAddLineToPoint(backgroundPath, NULL, _leftMarginToLeave, self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE);
CGPathCloseSubpath(backgroundPath);
backgroundLayer.path = backgroundPath;
graphLayer.path = graphPath;
circleLayer.path = circlePath;
CGPathRelease(backgroundPath);
CGPathRelease(circlePath);
CGPathRelease(graphPath);

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = 1;
animation.fromValue = @(0.0);
animation.toValue = @(1.0);
[graphLayer addAnimation:animation forKey:@"strokeEnd"];

backgroundLayer.zPosition = 0;
graphLayer.zPosition = 1;
circleLayer.zPosition = 2;

[self.layer addSublayer:graphLayer];
[self.layer addSublayer:circleLayer];
[self.layer addSublayer:backgroundLayer];

NSUInteger count2 = _xAxisValues.count;
for(int i=0; i< count2; i++){
    CGPoint point = plot.xPoints[i];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = [UIColor clearColor];
    btn.tag = i;
    btn.frame = CGRectMake(point.x - 10, point.y - 10, 40, 40);
    [btn addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    //objc_setAssociatedObject(btn, kAssociatedPlotObject, plot, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    [self addSubview:btn];
}
circleLayer = nil;
graphLayer = nil;
backgroundLayer = nil;

}

这是我的全部功能我在线上访问不好[self addSubview:btn];

1 个答案:

答案 0 :(得分:0)

将代码移动到枚举块

[plot.plottingValues enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSDictionary *dic = (NSDictionary *)obj;

        [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            int xIndex = [self getIndexForValue:key forPlot:plot];
            double height = self.bounds.size.height - BOTTOM_MARGIN_TO_LEAVE;
            double y = height - ((height / ([_yAxisRange doubleValue] + yIntervalValue)) * [obj doubleValue]);
            (plot.xPoints[xIndex]).x = ceil((plot.xPoints[xIndex]).x);
            (plot.xPoints[xIndex]).y = ceil(y);
        }];
    }];