Seriously need some help, very simple

Discussion in 'iOS Programming' started by chizzer2003, Dec 28, 2011.

  1. chizzer2003 macrumors member

    Joined:
    Oct 2, 2010
    #1
    Hello, I am very confused on the concepts of class, methods and instances. I have honestly looked through my objective-c book and searched the web for hours but no avail. Any help would be greatly appreciated. I have this very simple piece of code from my book and it would greatly help if one of you could explain what bits are the objects, methods and instances. Thankyou.

    Code:
    @interface Fraction: NSObject
    {
    int numerator;
    Int denominator;
    }
     
    - (void) print;
    - (void) setNumerator: (int) n;
    - (void) setDenominator: (int) d;
    
    @end
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    .. This is explained in nearly every book :eek:
    But because i'm in a good mood i'll show you around

    Code:
    // Here you declare the name of your CLASS, which is a subclass of //NSObject, which is an standard objective-C (& Cocoa Touch class)
    @interface Fraction: NSObject
    {
    /* Here you declare your ivars (instance variables), with a int as object, and numerator as your name (and you have Int instead of int, something wrong here..)*/ 
    int numerator;
    Int denominator;
    }
     /* Here you define your public methods, if you import this class in another class, these methods will be shown public, and can be accessed, these should not be mistaking with Static methods */
    /* The void is the return type, here void(nothing), and (int)n are the parameters the methods wants to receive */
    - (void) print;
    - (void) setNumerator: (int) n;
    - (void) setDenominator: (int) d;
    
    @end
    Not sure if it's 100% correct, I never went to high school or anything, so learned everything myself.
     
  3. chizzer2003 thread starter macrumors member

    Joined:
    Oct 2, 2010
    #3
    Thankyou so much, this really helps! Will defo get me on my way, just couldn't get my head around it. Cheers! :)
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Which book do you have? Title, author, edition, if you please.
     
  5. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    I'm sorry, but if this is hard, and you oversaw that in a book >.> what did you pick up (no personal harm, just a question)
    This is just OOP, but in a different syntax.
    But no problem after all :)
     
  6. chizzer2003 thread starter macrumors member

    Joined:
    Oct 2, 2010
    #6
    Programming in Objective-C - Stephen G. Kochan.

    It was just hard for me to get my head around thats all, he used some rather odd analogies.
     
  7. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #7
    I always found it best to think of classes like real world objects. Think of a coffee mug as an object.

    Mug - is your class

    You have some instance variables like color, size, emotional attachment.
    You have some methods like fillMug, emptyMug, breakMug.

    Code:
    @interface Mug: NSObject
    {
    Color color;
    int size;
    int emotionalAttachment;
    }
    - (void) initWithColor:(Color *)color andSize:(int)size andEmotionalAttachment:(int)emotionalAttachment
    - (void) fillMug;
    - (void) emptyMug;
    - (void) breakMug;
    
    @end
    
    Each instance of your Mug class will have the same exact actions or methods you can perform on it but most of them will have different values in the instance variables.

    Now you can create all your Mugs.
    Code:
    Mug *worldsBestDad = [[Mug alloc] initWithColor:white andSize:large andEmotionalAttachment:priceless];
    Mug *anotherCatMug = [[Mug alloc] initWithColor:pink andSize:enormous andEmotionalAttachment:expendable];
    And call your methods.
    Code:
    [worldsBestDad fillMug];
    [anotherCatMug breakMug];
    *excuse any broken syntax, the forum compiler is not working :D
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    The best way to really understand is, is to download the Stanford University videos, the intro's to Objective-C etc, they will explain to you in the letter how this syntax and OOP works.
     
  9. chizzer2003 thread starter macrumors member

    Joined:
    Oct 2, 2010
    #9
    thanks a lot guys! :) set me on the path to actually be able to do something useful with computers rather than just recklessly mess around with them haha ;)
     

Share This Page