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开发者_JS百科" 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?
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.
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.
The files don't interact at all, using them is merely a convention, you could also put everything in the main.m
file.
A good starting point for learning Objective-C is the introduction to the Objective-C language.
In a nutshell, an Objective-C class is a C struct. An instance is a reference to such a struct that has been allocated in memory. A class has a name and an instance has a state or value.
The thing that sets an Objective-C class apart from a C struct is the ability to look up method addresses by name. In simplified terms, the struct has a hash table of function pointers keyed by name.
There are lots of other niceties in Objective-C objects, like reference counting, but calling methods by name is the crux of it. A SEL is a C string, but a C string is not a SEL.
As far as header and source files, by convention you declare the interface to a class in header files and define the methods in a source file. Defining things, other than types and constants, in a header file is a bad practice, as is including source files. You are free to declare anything you want in a source file, but it is essentially private to the source file.
A C executable, and thus an Objective-C executable, has an entry point at the main function. By convention main is defined in a file with the same name in Objective-C projects.
The below lines of code in Fraction.h are not nothing but getter methods. They are not redefining the two int variables declared above them.
- (int) numerator;
- (int) denominator;
精彩评论