Objective-C Instances and Methods Question

Discussion in 'iOS Programming' started by knif3r2, May 26, 2012.

  1. knif3r2, May 26, 2012
    Last edited: May 26, 2012

    knif3r2 macrumors newbie

    Joined:
    May 20, 2012
    #1
    So I've been reading the "Programming in Objective-C Fourth Edition" book and it doesn't really explain how Instances and Methods work. At least into a way I can understand it.

    Here is an example it told me to type out from the book. Thing is I don't understand a few key features. Can anyone help?
    Code:
    #import <Foundation/Foundation.h>
    
    @interface Fraction : NSObject
    
       -(void)  print;
       -(void)  setNumberator: (int) n; // <--- WHY DOES THERE NEED TO BE "n"?
       -(void)  setDenominator: (int) d; // <--- WHY DOES THERE NEED TO BE "d"?
    
    @end
    
    @implementation Fraction
    {
       int numerator;
       int denominator;
    }
    
    
    
    -(void) print
    {
       NSLog(@"%i/%i", numerator, denominator);
    }
    
    -(void) setNumerator:(int)n
    {
       numerator=n;
    }
    
    -(void) setDenominator:(int)d
    {
       denominator=d;
    }
    
    @end
    
    int main (int argc, char * argv[])
    {
       @autoreleasepool {
          Fraction *myFraction;          // <-- What does this section do?
                                              
          myFraction = [Fraction alloc];   // <--
          myFraction = [myFraction init]; //  <--
          
          [myFraction setNumerator:1];
          [myFraction setDenominator:3];
          
          NSLog(@"The value of myFraction is:");
          [myFraction print];
          
       }
       return 0;
    }
     
  2. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Basically, you are allocing a pointer, this frees memory so your pointer can be stored. So that's what an alloc does, init, you set the memory of the pointer to there -> so the storage i just talked about. Once you have the pointer, called myFraction here, you can do setters/getters and/or methods like your print function here.
    Hope that helped.

    Noxx
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Code:
    Fraction *myFraction; // I am going to have a pointer to a Fraction instance called myFraction.
    [myFraction alloc]; // I need memory to be set aside for an instance of a Fraction named myFraction.
    [myFraction init]; // Set up myFraction to initial values.
    Something like that...

    All three could be done in a single line like this:
    Code:
    Fraction *myFraction = [[Fraction alloc] init];
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    You are defining a variable name that will be used locally within the method that gets called. A variable is a reference to some data. Think about it, how else would your method deal with the value being passed if it didn't have a reference to it.


    I've numbered the comments so I can give longer descriptions below.

    Code:
    int main (int argc, char * argv[])
    {
       @autoreleasepool {
          Fraction *myFraction;         [COLOR="Red"]<-- 1.[/COLOR]
                                              
          myFraction = [Fraction alloc];   // [COLOR="red"]<-- 2.[/COLOR]
          myFraction = [myFraction init]; //  [COLOR="red"]<-- 3.[/COLOR]
          
          [myFraction setNumerator:1];
          [myFraction setDenominator:3];
          
          NSLog(@"The value of myFraction is:");
          [myFraction print];
          
       }
       return 0;
    }
    1. The compiler is reserving memory for a pointer. In a strict 64-bit system that would be 8 bytes long. When your app starts up, part of the memory it takes up includes that space. When you use the myFraction variable, it uses the information at that location as a pointer to where the real object is. See point 2.

    2. alloc is a base method that knows how to assign memory. Given the class you are using to create an object, alloc reserves a contiguous block of memory space to hold your instance variables. There maybe extra space required for behind the scenes object information.

    3. init and its variants (as seen below) are used to initialize your instance variables and perhaps do other required tasks that you want done before you start to use the object. When it is not possible to set certain information from within the init method, you'll often see calls to the new object immediately after it has been initialized. For instance (and read the comments);

    Code:
    myVT = [UITableView alloc]; // allocate memory for the object and return with the pointer to the start of that memory location to assign it to myVT.
    myVT = [myVT initWithFrame: myFrame style: myStyle]; // The object has been setup so now we initialize before use.
    
    // Further setup of the object before we start using it. These items couldn't be figured out in the initialization process.
    [myVT setDelegate: self]; // set the delegate .
    [myVT setDataSource: myDataSource]; // set the data source for dynamic use in a table view.


    ----------

    You are not freeing memory, you are reserving memory. I suspect something got lost in translation. I see someone hit the downgrade button on your post also while I was writing my response. Perhaps this is why.
     
  5. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #5
    Hmm, why did I get downgraded on a post i liked to help a user.. really confused on that, but don't really care, since I hope I can get to people who are actually in need to know the truth. I made a small mistake, and thank you for pointing that out, you are indeed reserving memory to set the memory on that block, you're answer was more indept, so i'd +1 yours.
     
  6. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    If the person knew the small mistake, they should have pointed it out rather than simply use a -1 with no explanation. Clicking may be easier but mostly useless since it doesn't point to the disagreement in a constructive way. Hell, we're not talking politics here.

    Thanks for the +1 jnoxx.
     

Share This Page