Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting nested objects?
    text
    copied!<p>I am just trying to make sure I am getting things right as I move forward with Objective-C, two quick questions if I may:</p> <p>(1) Am I accessing the Position object correctly from within Rectangle? am I right to access the Position object contained within by the pointer I set in the init, or is there a better way?</p> <p>(2) In [setPosX: andPosY:] Which of the two ways of setting the Position instance variables is best, or does it really not matter?</p> <pre><code>// INTERFACE @interface Position: NSObject { int posX; int posY; } @property(assign) int posX; @property(assign) int posY; @end @interface Rectangle : NSObject { Position *coord; } -(void) setPosX:(int) inPosX andPosY:(int) inPosY; // IMPLEMENTATION @implementation Rectangle -(id) init { self = [super init]; if (self) { NSLog(@"_init: %@", self); coord = [[Position alloc] init]; // Released in dealloc (not shown) } return(self); } -(void) setPosX:(int) inPosX andPosY:(int) inPosY { //[coord setPosX:inPosX]; //[coord setPosY:inPosY]; coord.posX = inPosX; coord.posY = inPosY; } </code></pre> <h1>EDIT_01</h1> <p>Do I then call -(id)initWithX:andY: from the Rectangle object when I init it? and if so how do I go about setting posX and posY from within main()? or do I replace the init for rectangle with a further -(id)initWithX:andY: and pass the values through?</p> <pre><code>@implementation Rectangle -(id) init { self = [super init]; if (self) { NSLog(@"_init: %@", self); coord = [[Position alloc] initWithX:1234 andY:5678]; } return(self); } ... </code></pre> <p>cheers gary</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload