简体   繁体   English

从Objective-C开始:ARC还是ARC?

[英]Starting with Objective-C: To ARC or not to ARC?

According to Apple's ARC Documentation , there are a fairly significant number of changes to the way that one develops software when using ARC. 根据Apple的ARC文档 ,使用ARC时开发软件的方式发生了相当大的变化。

As a complete beginner to Objective-C, would it be better to start off with ARC disabled, with the idea that it would give me a better low-level understanding of what is going on behind the scenes? 作为Objective-C的完全初学者,开始使用ARC禁用会不会更好,因为它会让我对幕后发生的事情有更好的低级理解? Or has ARC essentially deprecated the 'old way' of doing things, to the point that it's not really worth spending time learning? 或者,ARC基本上不赞成做事的“旧方法”,以至于不值得花时间学习?

This is basically an opinion question, and is therefore fairly dangerous. 这基本上是一个意见问题,因此相当危险。

My Opinion is a qualified yes. 我的意见是合格的。 It is worth learning basic memory management. 值得学习基本的内存管理。 The qualification being don't get bogged down in it. 资格不会陷入其中。 Learn what ARC is doing for you under the hood with some very simple projects. 通过一些非常简单的项目了解ARC正在为您做些什么。 Once you have a basic understanding of how to handle memory management, ie how to avoid retain cycles(as jemmons alluded to they can still be a problem with ARC). 一旦你对如何处理内存管理有了基本的了解,即如何避免保留周期(正如jemmons提到的那样,它们仍然可能是ARC的一个问题)。 Once you have a basic grasp of memory management. 一旦掌握了内存管理的基本知识。 Start using ARC. 开始使用ARC。

Also as Jason Coco pointed out ARC handles memory management for (to put it simply) NSObject subclasses. 同样,Jason Coco指出ARC处理内存管理(简单地说就是)NSObject子类。 So all of the CF Objects you will still be handling yourself, if you need to use them. 因此,如果您需要使用它们,您仍将自行处理所有CF对象。

An excellent explanation about what ARC is doing for you under the hood can be found in the WWDC2011 Session 323 - Introducing Automatic Reference Counting. 关于ARC在幕后为您做些什么的一个很好的解释可以在WWDC2011会话323-介绍自动参考计数中找到。

But there are some other considerations that might steer your decision. 但是还有一些其他因素可能会引导您的决定。

What devices do you need to target? 您需要定位哪些设备?

If you plan to target iOS 4.3 and up ARC effectively handles memory management for you.(of NSObject subclasses) 如果您打算以iOS 4.3为目标,那么ARC会为您有效地处理内存管理。(NSObject子类)

If you plan to target iOS 4.2 then you will not be able to use weak references(you will use unsafe_unretained). 如果您计划定位iOS 4.2,那么您将无法使用weak引用(您将使用unsafe_unretained)。 iPhone 3g? iPhone 3g? & iPod touch 2nd gen are stuck at this OS level, because there are many of these devices still in service many developers are still targeting them. 和iPod touch第二代卡在这个操作系统级别,因为有许多这些设备仍在服务中,许多开发人员仍在瞄准它们。

If you plan to target iOS s earlier than 4.2(This would be rare) you will definitely need to learn MRC(Manual Reference Counting). 如果您计划将目标锁定在早于4.2的iOS (这种情况很少见),您肯定需要学习MRC(手动参考计数)。

If you plan to build Mac Apps, there is a garbage collector available on that platform. 如果您计划构建Mac应用程序,则该平台上可以使用垃圾收集器。 ARC is also an option(full ARC 10.7, no weak support 10.6). ARC也是一种选择(完整的ARC 10.7,没有弱支持10.6)。

It's worth noting that ARC is checked by default when you start a new project in Xcode. 值得注意的是,当您在Xcode中启动新项目时,默认情况下会检查ARC。 This is as good a sign as any that the old retain / release way of doing things is deprecated and Apple sees ARC as the future. 这与旧的retain / release方式被弃用以及Apple将ARC视为未来一样好。 Your first lesson as a new ObjC developer might be that it never pays to swim up-stream against Apple. 作为一个新的ObjC开发人员,你的第一课可能是向Apple上游游泳是不值得的。

Also, while it's fairly easy to convert old retain / release samples to ARC (for the most part, just drop any retain s, release s, and autorelease s), the inverse is not true. 此外,虽然将旧的retain / release样本转换为ARC相当容易(但大多数情况下,只删除任何retainreleaseautorelease ),反之则不然。 And I've already seen a lot of sample code cropping up that's written ARC-style. 而且我已经看到很多样本代码都是以ARC风格编写的。 So as someone just starting out, it'd pay more to learn the ARC way. 因此,当有人刚开始时,学习ARC的方式需要付出更多。

Note this doesn't mean you don't have to understand reference counting. 请注意,这并不意味着您不必了解引用计数。 It's still an important part of an object's life cycle and you still need to be aware of such things (if only to know when to use weak or strong references). 它仍然是对象生命周期的重要组成部分,您仍然需要了解这些事情(如果只知道何时使用weak引用或strong引用)。 But when it comes time to write code, write it with ARC on. 但是,当编写代码时,请使用ARC编写代码。

The "old" style is simply just reference counting to manage your object's lifetime. “旧”风格只是用于管理对象生命周期的引用。 There's not really all that much too it, but it can be error-prone and cause all kinds of grief. 它实际上并没有那么多,但它可能容易出错并导致各种悲伤。 If you're just starting, I'd personally suggest you just learn to program using ARC. 如果你刚开始,我个人建议你学习使用ARC编程。 You'll still get to deal with reference counting when you need to use the C-library objects, like CoreFoundation or CoreGraphics. 当您需要使用C库对象(如CoreFoundation或CoreGraphics)时,您仍然可以处理引用计数。

Apple itself recommends ARC for new projects. Apple本身为新项目推荐使用ARC。 http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html#//apple_ref/doc/uid/10000011i

read point 2 in At a Glance 阅读第2点概览

There is sooo much more interesting to learn in Objective C & iOS apart from the memory management. 除了内存管理之外,在Objective C和iOS中学习更有趣。 My advice : Don't bother about MRR 我的建议:不要打扰MRR

It would be a great idea just to go over it and understand what's going on, but for development it's not essential, and as you can see most if not all developers have moved their deployment targets to iOS 5.0+, so you are likely not to develop under manual reference counting. 回顾它并了解正在发生的事情将是一个好主意,但对于开发它并不重要,并且正如您所看到的那样,如果不是所有开发人员都将其部署目标移至iOS 5.0+,那么您可能不会在人工参考计数下开发。

However if you plan to use non ROP -retainable object pointers- in your code like CFStringRef, you might want to really look at non ARC so you can understand things like bridge, because you can combine ARC and non ARC code in one project. 但是,如果您计划在CFStringRef等代码中使用非ROP可获得的对象指针,您可能希望真正查看非ARC,以便您可以理解诸如桥之类的内容,因为您可以在一个项目中组合ARC和非ARC代码。

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

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