Objective C beginer question

Discussion in 'iOS Programming' started by larswik, Jun 23, 2010.

  1. macrumors 68000

    Sep 8, 2006
    Have limited Python programming experience and started learning Objective C and have a couple questions before I move on in the book 'Prog. in Obj-C'.

    I understand purpose of Objects and instances of Objects. I need help with part of this.
    @interface Fraction: NSObject
    	int numerator;
    	int denominator;
    -(void) print;
    -(void) setNumerator: (int) n;
    -(void) setDenominator: (int) d;
    The first line creates the Object named Fraction - I get that.
    The Object has 2 variables of type integer - I get that.
    I read and understand the next part with the -(void) setNumerator: (int) n; and how all that works, but what I don't get is why that code is in the Object? These come up again in the next part of the code called '@implementation Fraction'. Are these 3 -(VOID) lines kind of like doorways to pass the information to and from instance Objects?

    I hope I was not to confusing in trying to explain this.

  2. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Actually that line defines a class named Fraction; it doesn't create an object (or instance) of that class.
  3. thread starter macrumors 68000

    Sep 8, 2006
    Sorry that is what I meant. It defines not creates. The 'init' and 'alloc' reserves the memory and creates the instance if I read correctly.

  4. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Have you read "The @interface Section" yet? Also, you may want to go back and re-read the "Instances and Methods" section from earlier in the chapter.
  5. macrumors newbie

    Jun 23, 2010
    I guess the easiest way to explain it, is saying:

    Class Fraction is defined.

    Each object of class Fraction has 2 int variables, and 3 void methods.
  6. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    I would phrase it: 2 instance variables of type int and 3 instance methods with void return types.
  7. thread starter macrumors 68000

    Sep 8, 2006
    I did read the @interface Section starting page 33 and that is where I started to get lost. The first couple tutorial before this section I understood with no problem. There was a lot to take in starting at the @interface section for a few pages.

    I think I get the 3 Voids now from your last reply. They are the instance methods for the class Fraction that I am defining. Exmpl "Fill the car, Wash the car, drive the car." I missed the part that the VOIDS are the instance methods and they need to be there.

    Let me make sure I understand with a couple questions.
    1. If I define a Class with NO instance methods it won't work because it can't transfer values to it to be stored in the variables.

    2. In order for the int numerator; variable in the Class that I am defining to store a value I have to have the -(void) setNumerator: (int) n; which will pass the argument to the int numerator;..... If the -(void) setNumerator: (int) n;
    did not exist as the instance method then I could not pass a value to the class that I am defining.

    Am I correct in understanding that?

    Thanks again. I want to wrap my head around this section before reading on in the book.
  8. macrumors 68030


    Sep 2, 2008
    Just to be pedantic the @interface block DECLARES a class. The DEFINITION is the source code for that class, which goes in the @implementation block.

    Two of the methods in this class declaration are setters. They serve to set properties of the instances of this class.

    Your comments about "won't work" and "could not" are strictly speaking not correct. I'm not sure if the exceptions to your statements are too important for a beginner to understand. These setters are meant to set ivars.

    However not all setters need to set an ivar. Imagine a class that represents a database. It could have setters where the values go into the database, not into ivars. Imagine a class that stores all its values in a container object, like a dictionary or an array. Its setters could modify the container but not directly save to an ivar. There are also other ways to set ivars than directly through setters.

    Suffice it to say that these particular setters serve the purpose of setting these ivars. This is certainly a common way of doing things.
  9. smb
    macrumors newbie

    Jul 10, 2009

    You might want to check out the forum for this book. Steve Kochan usually posts answers to questions there about the book: http://classroomm.com/objective-c/
  10. thread starter macrumors 68000

    Sep 8, 2006
    Thanks for the link and additional info. I reread the section last night again and I'm starting to get the 3 different sections.

  11. macrumors 6502


    Jun 9, 2004
    Don't know if you've got it sorted out yet, so just in case:
    -(void) print; - Defines a method that's called print. (void) simply tells the compiler that the method will not return any variable or object, as opposed to for instance: -(int) print; which would be expected to return an integer.

Share This Page