现在的位置: 首页 > 综合 > 正文

Xcode中使用NSAutoreleasePool报错的问题

2011年04月20日 ⁄ 综合 ⁄ 共 448字 ⁄ 字号 评论关闭

今天在练习Learning Objective-c on Mac上的代码时输入了以下的代码,

NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];

//omitted

[pool drain];
return 0;

程序报错为"NSAutoreleasePool is unavailable: not available in automatic reference counting mode".

经过搜索得知Learning Objective-c on Mac使用的是比例老Xcode版本,而在Xcode4.2之后的版本中引入了ARC特性来自动管理内存,所以应该将下面的代码发为下面的样子。

@autoreleasepool{
//omitted.
}

PS:网上还有人建议在设置中关闭ARC来继续使用NSSAutoreleasePool,想不明白为什么有这个需求,难道是老代码用新Xcode来编译?如果是新项目的话还是尽量使用ARC吧,毕竟能自动管理内存是多美好的一件事啊。

抱歉!评论已关闭.