简体   繁体   English

在启用ARC的情况下使用保留/释放的情况是什么?

[英]What are the cases to use retain/release having ARC enabled

Having created a new iOS project I've enabled ARC and not planning to support iOS < 5.0. 创建了新的iOS项目后,我启用了ARC,并且不打算支持iOS <5.0。 Does that mean I'll never need to use retain/release or there are cases that might need using them? 这是否意味着我永远不需要使用retain/release或者在某些情况下可能需要使用它们?

Can someone please list these cases if there are any, thanks! 有人可以请列出这些情况,谢谢!

You should read Apple's migration guide. 您应该阅读Apple的迁移指南。 There are a few caveats. 有一些警告。 However, there is no situation in which you have to use retain/release yourself, except if you work with Core Foundation directly or if you specifically mark individual files with -fno-objc-arc and take ownership in releasing the memory yourself for that file only. 但是,除非您直接使用Core Foundation或使用-fno-objc-arc专门标记单个文件并拥有所有权为该文件释放内存,否则您不必使用retain/release自己的情况只要。

You will never need to use retain/release/autorelease. 您将永远不需要使用保留/释放/自动释放。 Having ARC enabled, frees you from writing memory management code, unless you're working with Core Foundation - ARC doesn't care about Core Foundation objects. 启用ARC可以使您无需编写内存管理代码,除非您使用的是Core Foundation-ARC不在乎Core Foundation对象。 But, you can let ARC release CF objects for you with a __bridge_transfer cast (or CFBridgingRelease). 但是,您可以使用__bridge_transfer强制转换(或CFBridgingRelease)让ARC为您释放CF对象。 If you get it from a Cocoa or Cocoa Touch function or method, it's in Objective-C-land and therefore managed by ARC. 如果您是从Cocoa或Cocoa Touch功能或方法中获得的,则它位于Objective-C领域,因此由ARC管理。 You can transfer it to the CF world with a __bridge_retained cast (or CFBridgingRetain), after which you'll have to CFRelease it (or transfer it back to ARC). 您可以使用__bridge_retained强制转换(或CFBridgingRetain)将其传输到CF世界,之后必须CFRelease(或将其传输回ARC)。 And yes, as long as the classes are compiled without ARC (which you can control on a file by file basis; go to Build Phases and add -fno-objc-arc as a flag to any file that should be compiled in an otherwise ARC'd project), then the compiled classes can override retain/release/autorelease to their heart's content. 是的,只要类是在没有ARC的情况下编译的(您可以逐个文件地对其进行控制;请转到“构建阶段”,然后将-fno-objc-arc作为标志添加到应在其他情况下通过ARC编译的任何文件中) 'd项目),则编译后的类可以覆盖其核心内容的保留/释放/自动释放。

No, you don't need to use them as ARC is enabled.

Retain/release are methods used retain and release a reference to an object, respectively. 保留/释放是分别用于保留和释放对对象的引用的方法。 It is used to manage memory allocation and deallocation. 它用于管理内存分配和释放。 User has to manage memory by himself, only when ARC is not enabled or not available as in below iOS 4.3. 仅当未启用ARC或iOS 4.3以下版本不提供ARC时,用户才必须自己管理内存。

ARC is Automatic Reference Counting . ARC是Automatic Reference Counting When enabled, SDK itself decides when to release an object. 启用后,SDK会自行决定何时释放对象。 User just need to allocate it. 用户只需要分配它。 User can still manage allocation of no. 用户仍然可以管理编号的分配。 of objects by declaring either a strong or weak reference to an object. 通过声明对对象的strong引用或weak引用来确定对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM