Objective-C Setter方法不起作用(简单)

时间:2013-07-25 06:36:55

标签: objective-c setter getter-setter nslog

我不能为我的生活找出为什么myRect.origin.x和myRect.origin.y显示的值为0.对于代码的广度道歉,我确实删除了一些明显不相关的东西但是没有在其他事情上确定无疑。另外,我知道我的交叉方法还没有正常工作,但我正在研究它。

//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值,程序仍然为myRect.origin的x和y值返回值0。

2 个答案:

答案 0 :(得分:2)

您的原点始终为零,因为您从未创建XYPoint对象。将它添加到Rectangle类:

- (id)init
{
    self = [super init];
    if (self) {
        origin = [[XYPoint alloc] init];
    }
    return self;
}

答案 1 :(得分:1)

您似乎永远不会创建XYPoint的实例,因此您向nil发送了大量邮件。

你应该使用CGRect(即使你的类是它的包装器),因为它会使你的代码和生活变得更加简单。