为什么 Xcode 4.2 在 main.m 中使用 @autoreleasepool 而不是 NSAutoreleasePool?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8714875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:53:15  来源:igfitidea点击:

Why does Xcode 4.2 use @autoreleasepool in main.m instead of NSAutoreleasePool?

iosxcodeautoreleasensautoreleasepool

提问by Foo

I've noticed that there is a different way in Xcode 4.2 to start the main function:

我注意到在 Xcode 4.2 中有一种不同的方式来启动 main 函数:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil,
                                 NSStringFromClass([PlistAppDelegate class]));
    }
}

and

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

Does anybody know the difference between those two?

有人知道这两者的区别吗?

采纳答案by Ignacio Inglese

The first one is using ARC, which is implemented in iOS5 and above to handle memory management for you.

第一个是使用 ARC,它在 iOS5 及更高版本中实现,为您处理内存管理。

On the second one, you're managing your own memory and creating an autorelease pool to handle every autorelease that happens inside your main function.

在第二个方面,您正在管理自己的内存并创建一个自动释放池来处理在主函数中发生的每个自动释放。

So after reading a bit on what's new on Obj-C with iOS5 it appears that the:

因此,在阅读了有关 iOS5 上 Obj-C 的新内容后,似乎:

@autoreleasepool {
    //some code
}

works the same as

工作原理与

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// some code
[pool release];

with the difference that the last one would throw an error on ARC.

不同的是,最后一个会在 ARC 上引发错误。

EDIT:

编辑

The first one is using ARC or not.

第一个是否使用ARC。