Amazon.com Widgets Do you understand it properly ? Help me please... Can't understand how it works
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 21, 2013, 01:43:49 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
| |-+  Exercises
| | |-+  Chapter 8
| | | |-+  Do you understand it properly ? Help me please... Can't understand how it works
Pages: [1]   Go Down
Print
Author Topic: Do you understand it properly ? Help me please... Can't understand how it works  (Read 514 times)
Justwarrior
Newbie
*
Posts: 25


Email




« on: June 22, 2012, 02:01:02 PM »

Hello, in the chapter 8 and in topic "Classes Owning Objects"  I can't understand how it works...

For example it says that we have to avoid problem that "pt" and "myPoint" has the same variables. So we change the code "-(void) setOrigin: (XYPoint *) pt {}"  to this :

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


But I didn't get that, how do that work ? Isn't the same thing as before ?

origin.x = pt.x and origin.y=pt.y  is the same as origin=pt right ?... omg HELP please
Logged
sd961960
Newbie
*
Posts: 3


Email




« Reply #1 on: June 30, 2012, 07:29:39 AM »

Hi JustWarrior, i too am a bit confused at times and think that these exercises are moving way too fast for what we learned thus far. Im amazed though at how many peeps are actually understanding this pretty readily considering how much thinking ahead is involved. Like in exercise 8.8 we use to nested for loops and then trying to keep the brain focused on the origin.x and origin.y as it goes through the for loop is maddening at times, often i can keep track in my mind of how the origin is changing each time through the loop and then its a mish mosh of numbers and symbols...i was cruising right along understanding everything till chapter 8, thats when i think Steve rushed his book. Anyway check out the Objective C videos on youtube by Bucky, they are basic but you might pick up some helpful pointers.
good luck
Logged
A15
Newbie
*
Posts: 7


Email




« Reply #2 on: July 04, 2012, 10:06:30 AM »

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.

Code: (Objective-C)
-(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).
Code: (Objective-C)
-(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
Code: (Objective-C)
       
 //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.
« Last Edit: July 04, 2012, 10:13:36 AM by A15 » Logged
sd961960
Newbie
*
Posts: 3


Email




« Reply #3 on: July 19, 2012, 04:39:04 PM »

I have to add this comment for anyone who is having trouble grasping some of the concepts with objective C,
 i too have some problems fully understanding some of these concepts. I really enjoy Steves book, and was understanding it quite well till chapter 8,
Then i have issues grasping it, but what saved me is Steve's video course which is his book but set video. It is extremely understandable. Steve does an amazing
job of explaining everything step by step, with zero ambiguity...the course is about 100 bucks but so worth it, i highly recommend it for learning Objective C.
Thanks Steve, you're an awesome instructor...please let us know if you do any other  videos.
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.