ホームページ  >  に質問  >  本文

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_lee2712日前371

全員に返信(2)返信します

  • 伊谢尔伦

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

    両者の行動時間は異なります。 AutoReleasePool オブジェクトの書き込みメソッドはランタイムで動作し、@autoreleasepool はコンパイル フェーズで動作します。 ARC を有効にしたい場合は、コンパイル段階で自動参照カウント管理を有効にするようにコンパイラに指示する必要がありますが、実行時に動的に追加することはできません。

    返事
    0
  • 高洛峰

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

    現在 Apple は、ARC が使用されているかどうかに関係なく、@autoreleasepool{} を使用することを推奨しています

    @autoreleasepool ブロッ​​クは、> NSAutoreleasePool のインスタンスを直接使用するより効率的です。ARC を使用しない場合でも使用できます。 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html

    返事
    0
  • キャンセル返事