我不能爲了我的生活找出爲什麼myRect.origin.x和myRect.origin.y顯示的值爲0.對於廣泛的代碼的道歉,我做了刪除有些事情顯然不相關,但在其他事情上並不確定。另外,我知道我的相交方法還沒有正常工作,但我正在處理它。Objective-C Setter方法不起作用(簡單)
//Rectangle.h
#import <Foundation/Foundation.h>
@class XYPoint;
@interface Rectangle: GraphicObject
@property float height, width;
-(XYPoint*) origin;
-(void) setOrigin: (XYPoint*) pt;
-(float) area;
-(float) perimeter;
-(void) setHeight: (float) h andWidth: (float) w;
-(void) translate: (XYPoint*) t;
-(BOOL) containspoint: (XYPoint*) aPoint;
-(Rectangle*) intersect: (Rectangle*) intr;
@end
@interface XYPoint: NSObject
@property float x,y;
-(void) setX:(float) xVal andY:(float) yVal;
@end
//Rectangle.m
#import "Rectangle.h"
@implementation Rectangle
{
XYPoint *origin;
}
@synthesize width, height;
-(float) area
{
return width*height;
}
-(float) perimeter
{
return 2*(width*height);
}
-(void) setHeight:(float) h andWidth: (float) w
{
width = w;
height = h;
}
-(void) setOrigin:(XYPoint *)pt
{
origin = pt;
}
-(XYPoint*) origin
{
return origin;
}
-(void) translate:(XYPoint *)t
{
origin = t;
}
-(BOOL) containspoint:(XYPoint *)aPoint
{
if (aPoint.x >= self.origin.x && aPoint.x <= self.origin.x + self.width && aPoint.y >= self.origin.y && aPoint.y <= self.origin.y + self.height) {
return YES;
}
else return NO;
}
-(Rectangle*) intersect: (Rectangle*) intr
{
Rectangle *zerorec = [[Rectangle alloc]init];
Rectangle *rec1 = [[Rectangle alloc]init];
Rectangle *rec2 = [[Rectangle alloc]init];
zerorec.origin.x = 0;
zerorec.origin.y = 0;
zerorec.height = 0;
zerorec.width = 0;
if ((self.origin.x >= intr.origin.x && self.origin.x <= intr.origin.x + intr.width) || (intr.origin.x >= self.origin.x && intr.origin.x >= self.origin.x + self.width))
{
if ((self.origin.y >= intr.origin.y && self.origin.y <= intr.origin.y + intr.height) || (intr.origin.y >= self.origin.y && intr.origin.y >= self.origin.y + self.height))
NSLog(@"The rectangles intersect");
//rec 1 = the rectangle with the greater x value
if (self.origin.x >= intr.origin.x && self.origin.x <= intr.origin.x + intr.width)
rec1 = self, rec1.origin = self.origin, rec2 = intr, rec2.origin = intr.origin;
else rec2 = self, rec2.origin = self.origin, rec1 = intr, rec1.origin = intr.origin;
Rectangle *returnedRec = [[Rectangle alloc]init];
returnedRec.origin.x = rec1.origin.x;
returnedRec.origin.y = rec2.origin.y;
//rec 1 = rectangle with greater y value
if (self.origin.y > intr.origin.y)
rec1 = self, rec2 = intr;
else rec1 = intr, rec2 = self;
returnedRec.height = rec1.height - (rec1.origin.y - rec2.origin.y);
returnedRec.width = rec1.width - (rec2.origin.y - rec1.origin.y);
return returnedRec;
}
else return zerorec;
}
@end
@implementation XYPoint
@synthesize x, y;
-(void) setX:(float) xVal andY:(float) yVal
{
x = xVal;
y = yVal;
}
@end
//main.m
#import <Foundation/Foundation.h>
#import "Rectangle.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Rectangle *returnedRect = [[Rectangle alloc]init];
Rectangle *myRect = [[Rectangle alloc]init];
Rectangle *yourRect = [[Rectangle alloc]init];
[myRect.origin setX:3 andY: 2];
[myRect setHeight:4 andWidth:5];
[yourRect.origin setX:6 andY:4];
[yourRect setHeight:7 andWidth:9];
myRect.origin.x = 2;
myRect.origin.y = 3;
returnedRect = [myRect intersect:yourRect];
NSLog(@"x = %f, y = %f", returnedRect.origin.x, returnedRect.origin.y);
NSLog(@"height = %f, width = %f", returnedRect.height, returnedRect.width);
NSLog(@"%f, %f", myRect.origin.x, myRect.origin.y);
}
return 0;
}
正如你所看到的,我嘗試使用setX的方法和Y與設置x和y的值明確,程序仍返回x和myRect.origin的y值的值爲0一起。
謝謝,本書中的這個問題來自於(我錯過了這一部分)增加了類似於你的答案的東西,但是在SetOrigin方法實現本身中,因爲我們還沒有作爲一種方法被初始化。我不明白的(以及我現在所理解的,下面的內容可能並不準確)是,作爲Rectangle類的一部分的原點只爲原點分配空間但不初始化它,因爲它是一個對象本身,它需要在可以使用之前進行初始化。 – Ronald
不完全是,當您調用類本身的alloc方法時,您的XYPoint的內存會被分配。通過將原點添加到您的Rectangle類中,您只需添加一個指向XYPoint對象的指針即可。 – shadowhorst