exc_bad_access错误//

时间:2012-06-18 16:41:53

标签: objective-c

我正在使用Xcode 4和ARC,当我在本书中尝试进行此练习时,我遇到了这个错误。 Thread 1: Program received signal: "EXC_BAD_ACCESS".位于Rectangle *intersectingRect = [[Rectangle alloc] init]; intersect方法的Rectangle.m行。

#import <Foundation/Foundation.h>

@interface XYPoint : NSObject 
@property float x, y;

@end

#import "XYPoint.h"

@implementation XYPoint 
@synthesize x, y;

@end

#import "XYPoint.h"

@interface Rectangle : NSObject  
@property float width, height;

-(void) setOrigin: (XYPoint *) pt;
-(XYPoint *) origin;
-(BOOL) containsPoint: (XYPoint *) aPoint;
-(Rectangle *) intersect: (Rectangle *) myRect;

@end

#import "Rectangle.h"

@implementation Rectangle {
    XYPoint *origin;
}
@synthesize width, height;

-(void) setOrigin: (XYPoint *) pt {
    if (! origin) 
        origin = [[XYPoint alloc] init];

    origin.x = pt.x;
    origin.y = pt.y;

}

-(XYPoint *) origin {
    XYPoint *myOrigin = [[XYPoint alloc] init];

    myOrigin.x = origin.x;
    myOrigin.y = origin.y;

    return myOrigin;
}

-(BOOL) containsPoint:(XYPoint *)aPoint {
    if (origin.x + height >= aPoint.x && origin.y + width >= aPoint.y)
        return YES;
    else
        return NO;
}

-(Rectangle *) intersect:(Rectangle *)myRect {
    Rectangle *intersectingRect = [[Rectangle alloc] init];


    if ([self intersect: myRect]){
        intersectingRect.width = self.origin.x + self.width - myRect.origin.x;
        intersectingRect.height = myRect.origin.y + myRect.height - self.origin.y;

        intersectingRect.origin.x = myRect.origin.x;
        intersectingRect.origin.y = self.origin.y;
    }
    return intersectingRect;
}

0 个答案:

没有答案