开发者

Simple self contained Objective C file

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 setNu开发者_StackOverflowm: (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 -

@implementation testing1;  // Semi colon shouldn't be present at the end

With that modification made, you should see the result. Online result of the program

[test setNum: (int)34];

Here typecasting 34 to int is unnecessary. You can just pass the message setNum to reference test with 34 followed by colon.

[test setNum:34 ];

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

#import <Foundation/NSObject.h>
@interface testing1 : NSObject
{
    int number;
}

-(void) setNum:(int)a;
-(void) print;
@end

testing.m

#import <stdio.h>
#import "testing.h"

// You should definitely import just stdio.h header here because of printf function usage
// testing.h also to be imported or else compiler doesn't know what is testing1.

@implementation testing1
-(void) setNum:(int) a{
    number = a;
}
-(void) print{    
    printf("this is the number %i \n", number);
}
@end

main.m

#import <stdio.h>
#import "testing.h"

int main(int argc, char* argv[] )
{
    testing1 *test =[testing1 new]; 
    [test setNum: 34];
    printf("testing");
    [test print];

     return 0;
}

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.

gcc -o test testing.m main.m -framework Foundation

Run -

./test


The book you're following is out of date. The Object class was effectively deprecated way back in 1994 with the introduction of OpenStep.

In Objective-C 2.0, the Object class simply isn't available at all. That's why your program produces compiler warnings/errors.

You should instead subclass NSObject and import <Foundation/Foundation.h>. Use the -framework Foundation gcc parameter, instead of -l objc.


First of all, you used #import <stdio.h>. You only need that when your using C only. Delete the sentence and use this instead.


#import <Foundation/Foundation.h>

Secondly, why are you using #import <objc/Object.h>? The "<" and ">" are only for frameworks. If you want to import another file you made, use #import "Object.h" instead. (Unless you're talking about NSObject, then you don't need to import anything because it is part of the Foundation framework.)

And lastly, you should take away the semicolon after the @implementation because it's unnecessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜