I’m trying to run through all the Objective C I possibly can as fast as I can for my new job. I’ve divided up my training so I do 1/2 with cocoa tutorials and I thought it also made sense to try to learn Objective C from a general perspective as well so the other 50% i’m trying to learn out of the “Programming In Objective C” book by Stephan Kochan (2004). Many of his examples up through the first 50 pages or so seem to imply that there is a way to make a simple class (interface, implementation, and main) all in the same file. I have been trying to compile this in the mac Snowleopard terminal for the sake of speed, but as soon as I call a method inside the main I get compile errors.
I know its not my gcc compiler because the file will compile, and I can even write a printf statement, but as soon as I try to call a method, I get compile errors. I would appreciate it if someone could demonstrate a simple inclusive objectivec.m file that actually works with a method call.
here’s my code
#import <stdio.h>
#import <objc/Object.h>
@interface testing1 : Object
{
int number;
}
-(void) setNum:(int)a;
-(void) print;
@end
@implementation testing1;
-(void) setNum:(int) a{
number = a;
}
-(void) print{
printf("this is the number %i \n", number);
}
@end
int main(int argc, char* argv[] )
{
//testing1 *test =[testing1 new];
//[test setNum: (int)34];
printf("testing");
//[test print];
}
this will compile in the terminal with — gcc tester1 -o myProg -l objc
I’ve tried to call those methods several different ways but it does not work
any help is appreciated. Perhaps I need to break it up and use make – I don’t know
Thanks
MIke
You have
;at the end of@implementation–With that modification made, you should see the result. Online result of the program
Here typecasting 34 to
intis unnecessary. You can just pass the messagesetNumto referencetestwith 34 followed by colon.Interface declarations should go in header while the implementation to source files. Only source files get compiled. Before even the compilation phase, pre-processor just copies the content of all imported files to the translation units. So,
testing.h
testing.m
main.m
Now you need to compile the two source files from which corresponding object files are generated. Linker combines these object files to give the final executable.
Run –