objective-c中@autoreleasepool的用法

objc中关于自动释放池,有两种语法,一种old-fashioned是:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//do something...
[pool release];

那么新的语法是:

@autoreleasepool{
    //do something
}

可以明显看出,后者比前者语法要简单,那么新式和旧式的语法有神马区别呢?
apple dev里有清楚的说明:
NSAutoreleasePool class用来支持Cocoa的引用计数内存管理系统。如果你使用ARC,你不能直接使用autoreleasepool,作为替代,你可以使用@autoreleasepool块的语法:

If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks. 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块的语法更有效率,所以你也可以在不使用ARC的情况下使用该语法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值