Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:03:09 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: allocate and initialize new object on page 165  (Read 1419 times)
bpran
Newbie
*
Posts: 18


« on: August 20, 2011, 03:41:11 AM »

I am getting confuse with the way object allocate and initialize :

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


why not use this (which I know will give error message);


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

XYPoint *origin = [[XYPoint alloc] init];
this format is what normally I see in the main.m when allocating object.
XYPoint *posXY = [[XYPoint alloc] init];   //this is in the main.m


P.S:
Sorry if my English is not good.
Logged
bpran
Newbie
*
Posts: 18


« Reply #1 on: August 20, 2011, 03:51:29 AM »

Is it because the XYPoint *origin already been declared in the .h file?

Code: (Objective-C)
@interface Rectangle : NSObject
{
     int witdh;
     int height;
     XYPoint *origin;   // <-------- this is what I talk about
}
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW
« Reply #2 on: August 20, 2011, 03:54:44 AM »

Yeah, you got it.  Smiley
Logged
bpran
Newbie
*
Posts: 18


« Reply #3 on: August 20, 2011, 07:26:43 AM »

Yeah, you got it.  Smiley

Thank you Smiley
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.