为什么Xcode 4.2在main.m中使用@autoreleasepool而不是NSAutoreleasePool? [英] Why does Xcode 4.2 use @autoreleasepool in main.m instead of NSAutoreleasePool?

查看:379
本文介绍了为什么Xcode 4.2在main.m中使用@autoreleasepool而不是NSAutoreleasePool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

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

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

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

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.

所以在阅读之后关于使用iOS5的Obj-C的新功能,看来:

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

@autoreleasepool {
    //some code
}

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

,区别在于最后一个会在ARC上抛出错误。

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

编辑

第一个是否使用ARC。

The first one is using ARC or not.

这篇关于为什么Xcode 4.2在main.m中使用@autoreleasepool而不是NSAutoreleasePool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆