NSAutoreleasePool 和 @autoreleasepool blocks 区别

本文探讨了在Xcode4.3引入ARC后,NSAutoreleasePool与@autoreleasepool的区别及使用场景。当使用ARC时,需将NSAutoreleasePool替换为@autoreleasepool,后者效率更高且即使不使用ARC也可采用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  main.m
//  HelloWorld
//
//  Created by Erica Sadun on 4/24/09.
//  Copyright __MyCompanyName__ 2009. All rights reserved.
/**
 xcode4.3引入ARC,release这块就有些变化,当你使用ARC,就必须将NSAutoreleasePool的地方换成 @autoreleasepool
 关于NSAutoreleasePool的解释官方的最清楚
 Important If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks instead. For example, in place of:
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init;
 // Code benefitting from a local autorelease pool.
 [pool release];
 you would write:
 @autoreleasepool {
 // Code benefitting from a local autorelease pool.
 }
 @autoreleasepool blocks are more efficient than using an instance of NSAutoreleasePool directly; you can also use them even if you do not use ARC.
 **/

#import <UIKit/UIKit.h>
#import "HelloWorldAppDelegate.h"  //如果下面第二个参数直接用nil,不用import这个Delegate.h头文件也可

int main(int argc, char *argv[]) {
    
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"nil如何调用窗口呢");

    int retVal = UIApplicationMain(argc, argv, nil,nil); //等效下一句,第二个nil表示就把模板生成的Delegate类作为默认参数
    
    //int retVal = UIApplicationMain(argc, argv, nil,NSStringFromClass([HelloWorldAppDelegate class]));

    [pool release];
    return retVal;
}

 

posted on 2012-05-17 15:36  人的本质是什么? 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/PirateCaptain/articles/2506330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值