Objective-c – Concise description of how .h and .m files interact in objective c

objective c

I have just started learning objective C and am really confused how the .h and .m files interact with each other. This simple program has 3 files:

Fraction.h

 #import <Foundation/NSObject.h>
    @interface Fraction : NSObject {
        int numerator;
 int denominator;
    }
    - (void) print;
    - (void) setNumerator: (int) n;
    - (void) setDenominator: (int) d;
    - (int) numerator;
    - (int) denominator;
    @end

Fraction.m

 #import "Fraction.h"
    #import <stdio.h>
    @implementation Fraction
    -(void) print { printf( "%i/%i", numerator, denominator ); }
    -(void) setNumerator: (int) n { numerator = n; }
    -(void) setDenominator: (int) d { denominator = d; }
    -(int) denominator { return denominator; }
    -(int) numerator { return numerator; }
    @end

Main.m

 #import <stdio.h>
    #import "Fraction.h"
    int main(int argc, char *argv[]) {
        Fraction *frac = [[Fraction alloc] init];
 [frac setNumerator: 1];
 [frac setDenominator: 3];
 printf( "The fraction is: " );
 [frac print];
 printf( "\n" );
 [frac release];
 return 0;
    }

From what I understand, the program initially starts running the main.m file. I understand the basic C concepts but this whole "class" and "instance" stuff is really confusing. In the Fraction.h file the @interface is defining numerator and denominator as an integer, but what else is it doing below with the (void)? and what is the purpose of re-defining below? I am also quite confused as to what is happening with the (void) and (int) portions of the Fraction.m and how all of this is brought together in the main.m file. I guess what I am trying to say is that this seems like a fairly easy program to learn how the different portions work with each other – could anyone explain in non-tech jargon?

Best Answer

People who come from other environments always seem to belive that something complicated is happening with the .c, .m, and .h files used in C and Objective-C programming.

Actually, its very, VERY simple.

For the purpose of buiding a project Integrated Development Environments - like XCode - ignore all the .h files. What they do do is to take each .c and .m file and compile it. If the programmer (thats you) has used any #include, or #import directives, the compiler inserts the entire text of the included/imported .h file where the directive was.

So, if you had a .h file - insert.h - that said:

in

And a .c file that said:

Alice
#include "insert.h"
Wonderland

The compiler would, after processing the #include & #import directives, see this:

Alice
in
Wonderland

It is this very VERY simple file merging behavior that we use to make complicated programs :)

.h is very simply a convention by which programmers can tell each other that the file is suitable to be merged in - potentially multiple times - using #include or #import.

The .c and .m files are not merged like that. Each .c and .m file is compiled seperately - to produce .o files. Each .o file is a collection of compiled functions. The .o files are then merged - or "linked" - to produce the final program. The linking step ensures that each function exists only once, and that all functions that are called do in fact exist somewhere.

C & Objctive-C define one special function that must exist somewhere - main(). Again, the language is very relaxed - it doesn't care which .c or .m file the main() function is in. Merely that it exists in some file somewhere.