1

我正在使用 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;
}
4

0 回答 0