私は 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;
}