Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  The Objective-C Language
Subject:   Question
Date:   2001-05-08 12:38:40
From:   uwiz
I have a question concerning the following syntax:


- (void)setXLocation:(double)x yLocation:(double)y


I don't understand the yLocation:(double)y part.
Isn't yLocation an instance variable in the given
example? Why is an instance variable listed in
a method declaration?


thanks



Full Threads Oldest First

Showing messages 1 through 7 of 7.

  • Michael Beam photo Question
    2001-05-08 15:20:23  Michael Beam | O'Reilly Author [View]

    P.S. In the method under scrutiny here, the name of the method without the arguments is:

    setXLocation:yLocation:

    So you see yLocation here isn't refering to the instance variable, but is just part of the name. Also, the reason we have uppercase "X" and lowercase "y" is purely a matter of aesthetics. If the arguments were reversed then I would have written an uppercase "Y" and lowercase "x":

    setYLocation:xLocation

    I just like having new words or standalong letters in variable and method names start with an uppercase letter. Purely a matter or preference.



    Sorry if this was redundant with my last message, i'm just trying to attack it from all angles.
  • Michael Beam photo Question
    2001-05-08 15:12:51  Michael Beam | O'Reilly Author [View]

    Yes, yLocation is an instance variable for the class. "yLocation" the text is also part of the method name described. The reason i choose this name is to provide information about what the argument following the colon sets. you could have replaced "yLocation:(double)y" in the method declaration with "locationOfYCoordinate:(double)y" if you wanted. I just choose "yLocation" because ultimately that is the instance variable being set to the value of the second argument in this method.

    hope that helps clear things up!

    Mike
    • Question
      2001-05-10 14:41:38  kcrawford [View]

      I'm still confused. You put the arguments inside the name of the method?

      Please show the same method written in Java so I can translate.

      Would it be:

      public void setXLocationyLocation(double x, double y){}
      • Michael Beam photo Question
        2001-05-11 06:13:54  Michael Beam | O'Reilly Author [View]

        that's right, you put arguments in the name of the method. Think of it as each argument having a label that is part of the method name. Your java line was correct, although i think you meant to put in a Y before the second location

        public void setXLocationYLocation(double x, double y) {}

        • Question
          2001-05-11 09:19:15  kcrawford [View]

          Thanks, that clears things up a bit.

          Would you call the method with multiple arguments the same way--with the arguments inside the method name?

          For example, would you call the method

          - (void)setXLocation:(double)x yLocation:(double)y

          like this

          [setXLocation:10 yLocation:20];
          • Michael Beam photo Question
            2001-05-11 12:00:20  Michael Beam | O'Reilly Author [View]

            Yeah, that's right, except your method call has no receiver, and it should. So it would have to be

            [anObject setXLocation:10 yLocation:20];

            when you want to invoke it.
            • Question
              2001-05-15 10:06:53  TheBum [View]

              Allow me to step in here with a related question if I may. Are the tags in the argument list of the declaration, e.g. yLocation, just for readability or do they provide a way of specifying arguments in any order?