Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concise description of how .h and .m files interact in objective c?

Tags:

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?

like image 501
RJ86 Avatar asked Apr 11 '10 23:04

RJ86


People also ask

What is .h and .m file in Objective-C?

h is called the Header file and the . m is called the Implementation file. Together, they form the AppDelegate class.

What is .m and .h files?

h file is a header file for public declarations of your class like an API, while the . m file is the private implementation. When you need to call a function at the other files, just need to import the .h files for reference.

What is .m file in Objective-C?

An M file is a class implementation file used by programs written in Objective-C. It begins with the @implementation directive and initializes variables and functions that can be referenced by other Objective-C source files. M files may also reference header (.

What is a .m file in Xcode?

Since M files are objective-c files, they are used for the implementation of Apple's Mac OS X and iOS operating system applications. There are many applications that can open M files and one of the most popular applications used is the Apple Xcode by which M files are considered part of its projects.


2 Answers

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.

like image 194
Chris Becke Avatar answered Sep 18 '22 20:09

Chris Becke


You need to take a look into Object Oriented Programming and perhaps read a little more into Objective-C development to get a good grasp on the concepts of OOP etc

To answer your question "what is the difference between .h and .m files", .h files contain the declaration for your class, so basically all of attributes and methods that it can utilise. The .m file is the implementation of these methods.

In laymans terms, the header file (.h) is the a way of saying "This is what I can do" and the .m is "This is how I do it". It's a little more complicated then that though.

like image 29
djhworld Avatar answered Sep 17 '22 20:09

djhworld