Amazon.com Widgets Question about Prog. 8.5B + new setOrigin: method
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 01, 2014, 11:34:09 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 8
| | |-+ Question about Prog. 8.5B + new setOrigin: method
Pages: [1] Go Down
Print
Author Topic: Question about Prog. 8.5B + new setOrigin: method (Read 1513 times)
MCaEd
Newbie
*
Posts: 49






on: February 23, 2012, 06:42:19 PM

Hi,
I don't understand how the previous setOrigin:
Code: (Objective-C)
-(void) setOrigin: (XYPoint*) pt
{
   origin = pt;
}
is different from new one:
Code: (Objective-C)
-(void) setOrigin: (XYPoint*) pt
{
   if (! origin)
       origin = [[XYPoint alloc] init];

   origin.x = pt.x;
   origin.y = pt.y;
}
I understand why we alloc and init here. Because in the implementation file we have (XYPoint *) origin;, right?
But how was origin allocated and initialized with the old method then?!

And how new method solve the problem?
Isn't it the same when we have:
Code: (Objective-C)
origin.x = pt.x;
origin.y = pt.y;
with:
Code: (Objective-C)
origin = pt;
?!
How does the new method change the pointer? Because after we set new values to myPoint we don't invoke setOrigin any more, so why it doesn't change origin's values?
Thanks
Last Edit: February 23, 2012, 06:53:19 PM by MCaEd Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #1 on: February 23, 2012, 07:21:25 PM

In the old method, you're just copying the reference to the XYpoint object that already exists and is being passed as an argument.

In the new method, you're creating a new instance of an XYpoint object and copying the instance variables of the object that the argument is referring to, over to the new instance you just created.

So instead of copying a reference of an object, you created your own and copied the values of the instance variables.

Hope that's clear enough.
Logged
MCaEd
Newbie
*
Posts: 49






Reply #2 on: February 23, 2012, 07:43:13 PM

I understand what you are saying but not how this is done. It will be nice to have more detailed explanation, please.
Thanks
Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #3 on: February 23, 2012, 08:16:10 PM

Well, what part are you getting confused at with the code you showed?

In this case, you have an instance variable already created called origin. By origin = pt; you're just getting origin to point at pt(The object reference passed through as an argument.
Code: (Objective-C)
    -(void) setOrigin: (XYPoint*) pt  
    { 
       origin = pt; 
    } 


In this case, instead of having origin point to another object passed through as an argument, you're creating an new instance of XYPoint and having origin point to it with origin = [[XYPoint alloc] init]; Now that you have your new object, you're taking the object passed through as an argument and copying the values of its instance variables and assigning it over to the instance variables of the new object. This way, you can change the coordinates and have a very own copy of them, encapsulating them and only being able to change them by using a method.
Code: (Objective-C)

   if (! origin) 
       origin = [[XYPoint alloc] init]; 
 
   origin.x = pt.x; 
   origin.y = pt.y; 

Let me know specifically what you're having trouble with and I'll try to explain it the best I can.
Logged
MCaEd
Newbie
*
Posts: 49






Reply #4 on: February 24, 2012, 02:09:16 PM

Thank you for trying to help me, I really appreciate it.

Even though your last post made more sense, still a bit confused.

1. So when we use:
Code: (Objective-C)
   ...
   origin = pt;    
   ...
it means we point to object pt which is our myPoint. But when we change to a new method, those lines:
Code: (Objective-C)
   ...
   origin.x = pt.x;    
   origin.y = pt.y;
   ...
mean we just copy the values from myPoint (which is our object pt) and store in our new object? Is it right?

2. I'm confused with this:
Code: (Objective-C)
{    
   if (! origin)    
       origin = [[XYPoint alloc] init];
   ...
I understand why we don't have XYPoint *origin, since we have already have it earlier in the implementation part.
Code: (Objective-C)
@implementation Rectangle
{
   XYPoint *origin;
}
...
Right? But let's say, first time it checks if the origin is allocated and initialized, but we never checked with the old code, why is that?
Also, since now we copied the reference to our new object, it is not changed. But what if we use setOrigin: again, what will happen since origin won't be nonzero, will it change it?
Thanks
Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #5 on: February 24, 2012, 04:08:25 PM

The first part you got it right, when we just use the = sign, we only point to the object "pt".
Code: (Objective-C)
01.   ...  
02.   origin = pt;     
03.   ... 

In the second example, we no longer point to that object, but instead copy the variables from that object pt.x and pt.y into our very own origin.x and origin.y
Code: (Objective-C)
01.   ...  
02.   origin.x = pt.x;     
03.   origin.y = pt.y; 
04.   ... 


For the second part, I think I see where you're confused. You only had
Code: (Objective-C)
01.@implementation Rectangle  
02.{ 
03.   XYPoint *origin; 
04.} 
05....
before because we only needed a place holder(origin) for the reference to an object of XY *Point, not an actual instance of the object itself, so no need to alloc or init.

The only reason you see the origin = [[XYPoint alloc] init]; in the later code is because instead of just using origin to point to an object outside of the class, you're actually creating an instance of the object this time and copying over the values of the instance variables.
Logged
MCaEd
Newbie
*
Posts: 49






Reply #6 on: February 24, 2012, 04:26:46 PM

I see. So before we just use origin because it is a regular instance variable that store a reference. But later we want it to actually store the values. Right?
Last confusion, what happens if origin is nonzero and we enter setOrigin: method, will it change the values inside the origin? Or how does it work?
Thanks
Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #7 on: February 24, 2012, 06:37:49 PM

You can't really call it an "instance" before, because it was never allocated any memory with the alloc method. It's just a reference to what ever object is passed through as argument. Later is when it actually becomes an instance and it stores it's own variables.

The only reason that non zero test exists:
Code: (Objective-C)
    {    
       if (! origin)   
           origin = [[XYPoint alloc] init];   
       
       origin.x = pt.x;   
       origin.y = pt.y;   
    }   
Is to check if an instance of origin already exists(having memory allocated to it and such), so it doesn't try to create a new instance(allocating memory) every time the method is called to change the values of it's instance variables.
Logged
MCaEd
Newbie
*
Posts: 49






Reply #8 on: February 24, 2012, 07:11:41 PM

Ah, I see. Now I understand. Smiley Thank you, apexmutt.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.