Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 448839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:42:34+00:00 2026-05-12T21:42:34+00:00

I am just trying to make sure I am getting things right as I

  • 0

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:

(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?

(2) In [setPosX: andPosY:] Which of the two ways of setting the Position instance variables is best, or does it really not matter?

// 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;
}

EDIT_01

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?

@implementation Rectangle
-(id) init {
    self = [super init];
    if (self) {
        NSLog(@"_init: %@", self);
        coord = [[Position alloc] initWithX:1234 andY:5678];
    }
    return(self);
}
...

cheers gary

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:42:35+00:00Added an answer on May 12, 2026 at 9:42 pm

    (1) You’re accessing it correctly.

    (2) In objective-c 2.0, the assignments have identical affect.

    Design wise, you would want to make:

    -(void) setPosX:(int) inPosX andPosY:(int) inPosY;
    

    …into a method of Position. This encapsulates both the data and the methods related to into one object. So you could have calls like:

    coord = [[Position alloc] initWithX:inPosX andY:inPosY];
    

    or
    [coord setPosX:inPosX andPosY:inPosY];

    all much cleaner and easier to maintain.


    Edit O1

    Do I then call -(id)initWithX:andY:
    from the Rectangle object when I init
    it?

    That depends on your design. If the coord property is absolutely vital to the Rectangle instance, then you should call it when you initialize a Rectangle instance. You might even write an initializers for Rectangle that takes a position or x and y as input. eg:

    -(id) initWithPosition:(Position *) aPos {
        self = [super init];
        if (self) {
            NSLog(@"_init: %@", self);
            coord = aPos;
            // Released in dealloc (not shown)
        }
        return self;
    }
    

    You should also write a connivence initializer for the Position class:

    -(id) initWithX:(NSInteger) x andY:(NSInteger) y{
         self=[super init];
         self.posX=x;
         self.posY=y;
         return self;
    }
    

    You would then call like:

    Position *aPos=[[Position alloc] initWithX:100 andY:50];
    Rectangle *aRec=[[Rectangle alloc] initWithPosition:aPos];
    

    Or you could write another combination initializer for Rectangle:

    -(id) initWithXCoordinate:(NSInteger) x andYCoordinate:(NSInteger) y{
         self=[super init];
         Position *aPos=[[Position alloc] initWithX:x andY:y];
         self.coord=aPos;
         return self;
    }
    

    and call it like:

    Rectangle *aRec=[[Rectangle alloc] initWithXCoordinate:100 
                                            andYCoordinate:50];   
    

    These are rough examples but you get the idea. Objective-c gives you a lot of flexibility in setting up initializer so you can create any initializers you find convenient.

    You do generally want to avoid using actual functions instead of methods inside classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK. I just tied myself in a knot trying to make a form in
Just trying to get my head around Generics by reading this enlightening article by
Just trying to get my irb sessions to actually list the current line of
Just trying to get up to speed with the SDK... So, I've created my
Just trying to get my head round Spring and figuring out how I wire
I'm just trying to time a piece of code. The pseudocode looks like: start
Developing a website and just trying to get back into the swing of (clever)
I'm just trying to get a general idea of what views are used for
I am just trying to generate the classes for linq to mysql with dbmetal
I'm just trying to do simple scripting with F#, and Math.PI seems to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.