1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

New to Objective-C, a few newbie questions if you dont mind :)

Discussion in 'Mac Programming' started by s4yunkim, May 6, 2011.

  1. macrumors regular

    Just starting to learn Objective-C... it hurts my head but it's fun at the same time, so I guess it's not too bad.

    I just started on a section of a book about the OOP part of Objective-C, and I had two questions:

    1. It says in the book that:
    If that's true, why is it necessary to do the @interface part at all?

    2. Also, I notice that in the sample code, they do this:

    - (void) setFillColor: (ShapeColor) c
         fillColor = c;
    } // setFillColor
    They do this for other parts of the implementation(?) as well, but the variable "c" is "b", "a", and so on. I understand that this is done as to not cut off the scope of the old variable, but does it matter if the variable I use is the same as another part of the implementation?

    For example:

    - (void) setFillColor: (ShapeColor) c
         fillColor = c;
    } // setFillColor
    - (void) setBorderColor: (BorderColor) c
         borderColor = c;
    } // setBorderColor

    What problems would be caused by this?

    Thank you in advance for your help :)
  2. macrumors 65816


    So objects of other classes can "call" those methods of objects of the class being declared/defined. You should see the necessity for this once you start defining more than one class and objects for those classes start communicating with each other.

    There's no problem with this at all.

    The first c's scope starts at opening brace of setFillColor and ends at the closing brace of setFillColor. The scope of the second c starts at the opening brace of setBorderColor and end at the closing brace. This is, each c is local to their respective methods.

    They are completely different and separate c's. They have no relation to each other; except they have the same name, but that's only a similarity detected by us humans, to the compiler there is no similarity at all.
  3. macrumors regular


    That clears it up. Thank you for your great answers! :D

Share This Page