如何在Terminal命令行模式下运行Objective-C
一般Objective-C的代码都在Xcode中调试,今天实验了下如何在命令行模式下运行,还是比较简单的,记录分享一下。
File: xxd.h
#include <Foundation/Foundation.h>
@interface Xxd: NSObject
{
}
- (void) xxdSayHelloTo: (NSString *)name;
@end
File: xxd.m
#include <Foundation/Foundation.h>
#include "xxd.h"
@implementation Xxd
- (void) xxdSayHelloTo: (NSString *)name
{
NSLog(@"Hello World, %@", name);
}
@end
File: main.m
#include <Foundation/Foundation.h>
#include “xxd.h”
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id speaker;
NSString *name = @"buro79xxd";
xxd = [[Xxd alloc] init];
[xxd xxdSayHelloTo:name];
[xxd release];
[pool drain];
return 0;
}
运行一下:
xuexiaodong79 at Air in ~
$ gcc -framework Foundation main.m xxd.m -o xxd
xuexiaodong79 at Air in ~
$ ./xxd
2012-06-07 09:28:40.815 xxd[1459:707] Hello World, buro79xxd