0

I have a program that I would love to get help to fully understand.

XYPoint.h:

#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
@property float x, y;
-(void) setX: (float)xVal setY: (float)yVal;
@end

XYPoint.m:

#import "XYPoint.h"
@implementation XYPoint
@synthesize x, y;

-(void) setX:(float)xVal setY:(float)yVal
{
    x = xVal;
    y = yVal;
}
@end

Rectangle.h:

#import <Foundation/Foundation.h>

@class XYPoint;
@interface Rectangle: NSObject
@property float width, height, tx, ty;

-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) translate: (XYPoint *) point;
@end

Rectangle.m:

#import "Rectangle.h"
#import "XYPoint.h"

@implementation Rectangle
{
    XYPoint *origin;
}
@synthesize width, height, tx, ty;
-(void) setWidth: (float) w andHeight: (float) h
{
    width = w;
    height = h;
}

-(void) translate:(XYPoint *)point
{
    tx = origin.x + point.x;
    ty = origin.y + point.y;
}

-(void)setOrigin:(XYPoint *)pt
{
    //if (!origin)
     //  origin = [[XYPoint alloc] init];
    origin.x = pt.x;
    origin.y = pt.y;
}

-(XYPoint *) origin
{
    return origin;
}
@end

main:

#import <Foundation/Foundation.h>
#import "XYPoint.h"
#import "Rectangle.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {

        Rectangle *myRect = [[Rectangle alloc] init];
        XYPoint *vector = [[XYPoint alloc] init];
        XYPoint *origin = [[XYPoint alloc] init];

        [origin setX:10 setY:15];
        [myRect setOrigin:origin];

        [vector setX:30 setY:50];

        [myRect translate:vector];
        NSLog(@"%f , %f", myRect.tx, myRect.ty);

    }
    return 0;
}

In the Rectangle implementation there is a if (!origin) to test if the origin object was initialized, and if not, it will allocate and initialize it. I dont understand why when im using this test I get the right answer, and when not, i'm not getting the right answer..\ Could someone please explain a total newbie what is going on there? tnx

6
  • "origin" is just a pointer, not an object. Until it's initialized, there is no "there" there.
    – Hot Licks
    Feb 13, 2013 at 20:20
  • 1
    Because using the test, you are creating an XYPoint object (allocating and initialing it) and assigning its address to the XYPoint ptr. Without doing this, XYPoint *origin is of undefined value (which means by doing to origin.x and origin.y assignment you are probably stomping other data, or crashing. You could optionally implement the init method for Rectangle to it alloc's and init's origin at that time, then you wouldn't need to do the test.
    – David Hope
    Feb 13, 2013 at 20:21
  • @DavidHope thanks allot david for the detailed and great answer :)
    – MNY
    Feb 13, 2013 at 20:26
  • 1
    @DavidHope since origin is a class member it will be set to nil when the class is initialized, therefore, no stomping of other data will occur. Though variables are typically useless until initialized.
    – Joe
    Feb 13, 2013 at 20:29
  • 1
    @Joe sorry, too much C++ in my background. I didn't know exactly how Objective-C initializes, and my Mac is at home, so couldn't test it.
    – David Hope
    Feb 13, 2013 at 20:47

1 Answer 1

3

You answered it in your question:

if (!origin) to test if the origin object was initialized, and if not, it will allocate and initialize it

The if (!origin) is for lazy initialization and if you do not initialize it it will remain nil. Objective-C allows you to message nil without any result, so if you never initialize origin origin.x = pt.x; and origin.y = pt.y; does nothing and all of you math operations that rely on origin will use 0.0.

2
  • quick question: in this particular case i presented, isn't XYPoint *origin = [[XYPoint alloc] init]; in the main is allocating and initialize origin? and if i dont use the test (!origin), im not suppose to get the right answer? @Joe
    – MNY
    Feb 13, 2013 at 21:09
  • That allocates an origin inside of main completely unrelated to the origin inside of Rectangle. You are just copying the origin you created in main to the origin in Rectangle.
    – Joe
    Feb 14, 2013 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.