首页  >  问答  >  正文

objective-c - 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.

xcode4.3引入ARC,release这块就有些变化,当你使用ARC,就必须将NSAutoreleasePool的地方换成 @autoreleasepool

http://marshal.easymorse.com/archives...

ringa_leeringa_lee2761 天前406

全部回复(2)我来回复

  • 伊谢尔伦

    伊谢尔伦2017-04-21 11:18:28

    两者的作用时间不一样。AutoReleasePool对象的写法作用于运行时,@autoreleasepool作用于编译阶段。如果要启用ARC的话,在编译阶段就需要告诉编译器启用自动引用计数管理,而不能在运行时动态添加。

    回复
    0
  • 高洛峰

    高洛峰2017-04-21 11:18:28

    现在苹果推荐都使用@autoreleasepool{},无论是否使用ARC

    @autoreleasepool blocks are more efficient than using an instance of > NSAutoreleasePool directly; you can also use them even if you do not use ARC http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html

    回复
    0
  • 取消回复