iOS Objective-C Instances and Methods Question

knif3r2

macrumors newbie
Original poster
May 20, 2012
3
0
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;
}
 
Last edited:

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
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];
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
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,605
4,082
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];
 

xStep

macrumors 68020
Jan 28, 2003
2,012
98
Less lost in L.A.
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"?
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.


----------

Basically, you are allocing a pointer, this frees memory so your pointer can be stored.
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.
 

jnoxx

macrumors 65816
Dec 29, 2010
1,343
0
Aartselaar // Antwerp // Belgium
[/COLOR]

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.
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.
 

xStep

macrumors 68020
Jan 28, 2003
2,012
98
Less lost in L.A.
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.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.