初学Objective-C,在win7上搭建了GNUstep环境,编译了下《Programming in Objective-C》的示例代码,发现报错了:
代码:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSLog (@"Programming is fun!");
}
return 0;
}
编译报错:
改成网上找的另一段代码可以:
#import <Foundation/Foundation.h>
int main ( int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog(@ "\nHello,objective-C!\n" );
[pool drain];
return 0;
}
网上查了一下:
iOS 5新推出ARC(Automatic Reference Counting)功能,改进内存管理,使用了ARC则要用@autoreleasepool 代替NSAutoReleasePool;
而GNUstep 带的 gcc 编译器不支持 Objective-C 2.0 ,因此不支持 @autoreleasepool 这个指令。
此问题涉及Objective-C的垃圾回收和编译环境问题,待后续深入研究……