iOS 5的最佳实践(发布/挽留?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6308425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:17:08  来源:igfitidea点击:

iOS 5 Best Practice (Release/retain?)

iphoneiosios5memory-managementautomatic-ref-counting

提问by Geekgirl

As a beginning iPhone programmer, what is the best practice for writing apps to be used either with iOS 5 or older versions? Specifically, should I continue using the release/retain of data, or should I ignore that? Does it matter?

作为初学者的 iPhone 程序员,编写适用于 iOS 5 或更旧版本的应用程序的最佳实践是什么?具体来说,我应该继续使用数据的释放/保留,还是应该忽略它?有关系吗?

采纳答案by sudo rm -rf

It's up to you. You can write apps using ARC (Automatic Reference Counting), and Xcode will write "glue code" to allow your ARC enabled apps to run on iOS 4, no modifications required. However, certain things wont work, and most noticeably many libraries you might wish to use will (sometimes) throw up innumerable errors and you will be unable to use them until the developers release an update which is compatible with ARC.

由你决定。您可以使用 ARC(自动引用计数)编写应用程序,Xcode 将编写“粘合代码”以允许您启用 ARC 的应用程序在 iOS 4 上运行,无需修改。但是,某些事情将不起作用,最明显的是,您可能希望使用的许多库(有时)会引发无数错误,并且在开发人员发布与 ARC 兼容的更新之前您将无法使用它们。



Edit: I recently discovered that you can turn off ARC on a per-file basis. See pixelfreak's answer. So, my advice still stands, but now the 3rd-party libraries shouldn't need to be updated to work with ARC.

编辑:我最近发现您可以在每个文件的基础上关闭 ARC。请参阅pixelfreak的回答。因此,我的建议仍然有效,但现在不需要更新第 3 方库即可使用 ARC。

Here's what Apple says about opting out of ARC for specific files:

以下是 Apple 关于为特定文件选择退出 ARC 的说法:

When you migrate a project to use ARC, the -fobjc-arc compiler flag is set as the default for all Objective-C source files. You can disable ARC for a specific class using the -fno-objc-arc compiler flag for that class. In Xcode, in the target Build Phases tab, open the Compile Sources group to reveal the source file list. Double-click the file for which you want to set the flag, enter -fno-objc-arc in the pop-up panel, then click Done.

当您迁移项目以使用 ARC 时,-fobjc-arc 编译器标志被设置为所有 Objective-C 源文件的默认值。您可以使用该类的 -fno-objc-arc 编译器标志为特定类禁用 ARC。在 Xcode 中,在目标 Build Phases 选项卡中,打开 Compile Sources 组以显示源文件列表。双击要设置标志的文件,在弹出的面板中输入 -fno-objc-arc,然后单击“完成”。

enter image description here

在此处输入图片说明

See the full transition guide here.

请在此处查看完整的过渡指南。

回答by pixelfreak

For anyone still curious about how to turn off ARC on individual files, here's what I did:

对于仍然对如何关闭单个文件的 ARC 感到好奇的人,这就是我所做的:

  1. Go to your project settings, under Build Phases > Compile Sources
  2. Select the files you want ARC disabled and add -fno-objc-arccompiler flags. You can set flags for multiple files in one shot by selecting the files then hitting "Enter" key.
  1. 转到您的项目设置,在 Build Phases > Compile Sources
  2. 选择要禁用 ARC 的文件并添加-fno-objc-arc编译器标志。您可以通过选择文件然后按“Enter”键,一次性为多个文件设置标志。

I don't know if this is the recommended way, but it works for me.

我不知道这是否是推荐的方式,但它对我有用。

PS: I gathered this information from clang.llvm.org herewhich is publicly accessible, thus not under NDA.

PS:我从这里从 clang.llvm.org 收集了这些信息,该信息可公开访问,因此不受保密协议保护。

回答by nevan king

iOS 5 is still under an NDA, and probably will be until they release the public version. If you have a developer account, head over to the Apple Developer Forumsand ask there.

iOS 5 仍处于保密协议之下,可能要等到他们发布公共版本。如果您有开发者帐户,请前往Apple 开发者论坛并在那里询问。

For previous versions, you have to count references and retain and release accordingly. Check out the Memory Management guide.

对于以前的版本,您必须计算引用并相应地保留和释放。查看内存管理指南

Edit:Here's a public spec for Automatic Reference Countingand a quote from the public iOS 5 page:

编辑:这是自动引用计数公共规范公共 iOS 5 页面的引用:

Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

Objective-C 的自动引用计数 (ARC) 使内存管理成为编译器的工作。通过使用新的 Apple LLVM 编译器启用 ARC,您将永远不需要再次键入 retain 或 release,从而大大简化了开发过程,同时减少了崩溃和内存泄漏。编译器完全了解您的对象,并在不再使用的时候立即释放每个对象,因此应用程序运行速度与以往一样快,具有可预测的流畅性能。

回答by Dominic

The details are light/under NDA at the moment, but Apple has implemented Automatic Reference Counting (ARC) in iOS 5, as detailed here: http://developer.apple.com/technologies/ios5/

目前细节尚不明确/在保密协议下,但 Apple 已经在 iOS 5 中实现了自动引用计数 (ARC),详情如下:http: //developer.apple.com/technologies/ios5/

If you develop a new app in Xcode 4 with the iOS 5 SDK, you can safely ignore retain/release counting.

如果您使用 iOS 5 SDK 在 Xcode 4 中开发新应用程序,您可以放心地忽略保留/发布计数。

[edit] sudo rm -rf makes a good point; third party libs may be significantly affected

[编辑] sudo rm -rf 提出了一个很好的观点;第三方库可能会受到重大影响

回答by ThinkChris

No one mentioned SystemConfiguration.framework? Please don't forget to put it into Frameworks. I miserably spent several hours to realize it.

没有人提到SystemConfiguration.framework吗?请不要忘记将其放入Frameworks。我痛苦地花了几个小时才意识到这一点。

回答by Charles

It certainly is the choice of the developer or the team. ARC(Automatic Reference Counter) has made things a bit easier by automatically managing the memory for you. It will release, retain, and dealloc when appropriate. I do believe that you should gain experience managing the memory yourself preferably in a test application, if you haven't already. Another thing to consider is whether your application relies on third party libraries, which if not converted to ARC will prevent your application from compiling. The choice is obviously dependent on the situation at hand.

这当然是开发人员或团队的选择。ARC(自动参考计数器)通过自动为您管理内存使事情变得更容易。它将在适当的时候释放、保留和解除分配。我确实相信您应该在测试应用程序中获得自己管理内存的经验,如果您还没有的话。另一件要考虑的事情是您的应用程序是否依赖于第三方库,如果不转换为 ARC 将阻止您的应用程序编译。选择显然取决于手头的情况。

回答by Mubin Shaikh

set flag as -fno-objc-arc in project settings>Build Phases > Compile Sources

在项目设置>构建阶段>编译源中将标志设置为 -fno-objc-arc