过渡到 iOS 5 时需要注意什么

发布于 2024-12-09 23:31:05 字数 1909 浏览 1 评论 0原文

摘要: 您能否将迁移到 iOS 5 时需要注意的事项添加到我的清单中?在我升级到 iOS 5 的过程中,StackOverflow 的价值无可估量。我发现了一些在 Xcode 4.2 之前我错过的非常基本的东西,并且我想知道可能还潜伏着哪些其他“陷阱”。

详细信息:随着本周 iOS 5 的发布,我不得不对我的几个应用程序进行一些更改。由于新的 ARC 功能,Xcode 4.2 在分析内存管理代码方面做得更好。 iOS 5 更新是检查所有内存管理代码的好时机。新的编译器还发现了早期编译器遗漏的许多其他问题。感谢 Apple 编译器工程师。以下是有帮助的主要内容(其中许多也适用于早期版本的 iOS)。

  1. 确保在dealloc方法的末尾调用[super dealloc],而不是开始。
  2. 在 viewDidUnload 中,有些人报告了需要在以下位置调用 [super viewDidUnload] 的错误viewDidUnload 的结束,而不是开始。
  3. 了解保留计数、综合设置器以及何时调用释放或自动释放。新的编译器将比旧的编译器指出更多的问题。 (我以为我很小心,但显然我不够小心。)Apple 的 内存管理指南是必读的 --没有捷径。
  4. 最好在调试时打开僵尸(在 Xcode 中,选择 Product | Edit方案...并选择调试方案;在“诊断”选项卡上,选中“启用僵尸对象”。这可以帮助您找到僵尸的尝试使用(您不应该再使用的对象)。
  5. Leaks 工具也很有帮助。在配置文件模式下运行您的应用程序并选择泄漏模板。在“仪器”窗口中,选择“泄漏”仪器并选中“收集泄漏内存内容”框,它将帮助您查看代码中泄漏内存的来源。

我遇到了一些零碎的事情:

  • Apple的 单例模式需要将“oneway”添加到返回类型声明中:
    - (oneway void) release { }

对于我应该寻找的潜在陷阱,还有其他建议吗?我感觉我的应用程序现在更稳定了,但我以前对它们感觉很好。

Summary: Can you add to my checklist of things to watch out for when migrating to iOS 5? StackOverflow has been invaluable as I've worked on upgrading to iOS 5. I've discovered some pretty basic things I'd missed prior to Xcode 4.2, and I'm wondering what other "gotchas" might be lurking.

Detail: With iOS 5 shipping this week, I've had to make some changes to a couple of my apps. Xcode 4.2 does a much better job analyzing memory management code because of the new ARC feature. The iOS 5 update is a great point at which to review all your memory management code. The new compiler also finds a number of other issues that earlier compilers missed. Kudos to the Apple compiler engineers. Here are the main things that have helped (and many of them will also apply to earlier versions of iOS).

  1. Make sure to call [super dealloc] at the END of your dealloc methods, not the beginning.
  2. In viewDidUnload, some people have reported bugs that require [super viewDidUnload] to be called at the end, not the beginning, of your viewDidUnload.
  3. Understand retain counts, synthesized setters, and when to call release or autorelease. The new compiler will point out more problems than the older compilers did. (I thought I'd been careful, but apparently I wasn't careful enough.) Apple's memory management guide is required reading -- no shortcuts.
  4. It's a good idea to turn on zombies when debugging (in Xcode, choose Product | Edit Scheme... and select the Debug scheme; on the Diagnostics tab, check Enable Zombie Objects). This can help you find attempted uses of zombies (objects you shouldn't be using any more).
  5. The Leaks instrument is also helpful. Run your app in Profile mode and choose the Leaks template. In the Instruments window, select the Leaks instrument and check the box that says "Gather Leaked Memory Contents" and it will help you see where the leaked memory originates in your code.

There are a few odds and ends I've encountered:

    - (oneway void) release { }

Any other suggestions of potential pitfalls I should look for? I have a feeling that my apps are more stable now, but I felt pretty good about them before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2024-12-16 23:31:05

1/ 如果您更改模态控制器的大小,其行为会有所不同。如果您需要不同大小的模式对话框,请考虑使用 iOS 5 子视图控制器。

2/ 对于表格,如果您返回零节标题和正高度,则在 iOS 4 中,标题将被隐藏。在 iOS 5 中,您必须为 nil header 返回零高度。

3/ UDID 已弃用。您可以使用 CFUUIDCreate 创建一个唯一的 ID 并将其保存到您的设置中,但请注意,可以备份设备数据,然后将其恢复到另一个设备,从而使您拥有两个具有相同 ID 的设备。我通过将我的 id 保存到带有属性 kSecAttrAccessibleWhenUnlockedThisDeviceOnly 的钥匙串中解决了这种情况。

关于您的清单:
[super viewDidUnload] 应始终作为 viewDidUnload 中的最后一条语句调用。逻辑与[super dealloc]中的相同。请注意,您还应该在 dealloc 中调用 [self viewDidUnload] (如果您尚未释放内存),因为它不是隐式调用的(尽管有时是隐式调用的)。

根据我的实验,仪器中的泄漏检测不会报告在未分配属性名称的情况下合成的属性的泄漏。

1/ Modal controllers behave differently, if you were changing their size. If you need modal dialog of a different size, consider using iOS 5 child view controllers.

2/ For a table, if you were returning nil section header and positive height, in iOS 4, the header was hidden. In iOS 5, you have to return zero height for nil headers.

3/ UDID is deprecated. You can use CFUUIDCreate to create a unique id and save it into your settings but be aware that a device data can be backed up and then restored to another device, leaving you with two devices with the same id. I solved the situation by saving my id into keychain with attribute kSecAttrAccessibleWhenUnlockedThisDeviceOnly.

About your list:
[super viewDidUnload] should be always called as the last statement in your viewDidUnload. The logic is the same as in [super dealloc]. Note, that you should also call [self viewDidUnload] in your dealloc (if you don't already release your memory there) because it is not called implicitly (although sometimes it is).

From my experiments, leak detection in Instruments don't report leaks on properties which are synthesized without assigning a property name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文