JustWarrior,
Here's my best response. It's different because the new code is setting "origin" to its own XYPoint Object instead of simply making it a pointer to the XYPoint Object created by myPoint. This helps protect the myRect's origin from changes to myPoint's x,y coordinates.
--I hope my code shows up with the clean formatting; I'm new to this posting---
See in the Rectangle.m file we first created a setter method for Origin. This method takes in a XYPoint object and assigns it to the message receiver's Origin variable. In doing so it has set the receiver's (e.g. myRect) Origin variable to be a pointer to the argument's (e.g. myPoint) x,y coordinates located somewhere in the computer's memory. This can be a problem because if I make changes to myPoint it will cause the myRect's Origin to change. Read chapter 3 and I think you'll catch on; our two objects are referencing the same point in the computer's memory.
-(void) setOrigin: (XYPoint *) pt
{
origin = pt; //here pt is just a pointer to the myPoint XYPoint object
}
Now we will adjust the setOrigin method to help prevent changes to myPoint causing changes to myRect's Origin var. Here the author is doing a quick logic check to make sure the computer hasn't already allocated and initialized a "origin". If it has not been allocated and initialized then he goes head and does so. Now the Origin is actually its own XYPoint object with it's own spot in the computer's memory to story an x,y coordinate. This allows use to now set an "x" and a "y" point for origin thus the origin.x = pt.x (where pt is myPoint's x coordinate).
-(void) setOrigin: (XYPoint *) pt
{
if(!origin) //First make sure origin doesn't already have memory allocated to it
origin = [[XYPoint alloc] init]; //if origin does not already have memory allocated to it then we should go ahead and initialize one.
//Set the origin's x point equal to myPoint's x coordinate; but remember origin is its own object and has its own location in the computer's memory
origin.x = pt.x;
origin.y = pt.y;
}
So now with the adjustments to setOrigin we can run a test program that creates our myRect and myPoint objects (two separate locations in memory). Then set myRect to a given width and height, followed by setting myPoint to a given X,Y coordinate. We then tell the system that we want to set myRect's origin to be equal to myPoint's X,Y coordinates. We print the results, but then we decide that we want to set myPoint's X,Y coordinate to (50,60). Doing so will not affect myRect.origin's XY coordinate because we have changed setOrigin to create a separate object for myRect's origin; it's no longer a pointer to myPoint
//XYPoint example
Rectangle *myRect = [[Rectangle alloc] init];
XYPoint *myPoint = [[XYPoint alloc] init];
[myRect setWidth: 5.3 andHeight:8.4]; //Set myRect width and height
[myPoint setX:100 andY:200]; //Set myPoint's XY coordinates
myRect.origin = myPoint; //now set myRect.origin equal to the myPoint this is the same as statement [myRect setOrigin: myPoint]
NSLog (@"myPoint Coordinates at (%f, %f)", myPoint.x, myPoint.y); //outputs "myPoint Coordinates at (100,200)"
NSLog (@"myRect Origin at (%f, %f)", myRect.origin.x, myRect.origin.y); //outputs "myRect origin at (100,200)"
//Quick remember; we use "myRect.origin.x" because the origin is a "nested" object within the myRect object....hope that makes sense
[myPoint setX:50 andY:60]; //Now let's change myPoint's XY Coordinates, but this time we are not going to set "myRect.origin=myPoint" thus myPoint's origin won't be changed.....originally this would have changed myRect's origin b/c "origin" was just a pointer to the same spot in memory where myPoint.x and myPoint.y where stored.
NSLog (@"myPoint Coordinates at (%f, %f)", myPoint.x, myPoint.y); //outputs "myPoint Coordinates at (50,60)"
NSLog (@"myRect Origin at (%f, %f)", myRect.origin.x, myRect.origin.y); //outputs "myRect origin at (100,200)"
I hope this helps some. I struggled with this topic until I reread chapter 3.