关于iOS5 SDK中自动引用计数的一些问题 [英] Some questions about Automatic Reference Counting in iOS5 SDK

查看:102
本文介绍了关于iOS5 SDK中自动引用计数的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为iPad开发应用程序。 iOS 4.2开始开发,现在继续(我认为将完成)iOS 4.3。
我刚刚读到iOS 5中的ARC,基本上我明白我们将永远不再需要释放和保留对象。我的问题是:


  1. 如果我决定升级到iOS 5,我是否需要删除所有我的代码中的[myObject retain] [myObject release] 语句?


  2. <如果我使用ARC开发适用于iOS 5的新应用程序,我是否需要实施某种复古兼容性检查?即:我需要检查iOS版本并相应地调用retain和release吗?那么,基本上,ARC适用于所有iOS版本还是适用于iOS 5?



解决方案


如果我决定升级到iOS 5,我需要从$ b中删除所有[myObject retain]
和[myObject release]语句$ b我的代码?


是的,但XCode 4.2包含一个新的迁移到Objective-C ARC工具(在Edit-> Refactor菜单中,它可以为您完成。调用dealloc是一个不同的故事。正如评论中所提到的,clang引用指出你应该保留你的dealloc方法:



理由:即使ARC自动销毁实例变量,仍然是合法的编写dealloc方法的原因,例如释放不可保留的资源。在这种方法中未能调用[super dealloc]几乎总是一个错误。


使用新的启用ARC - fobjc-arc
编译器标志。对于Mac OS X v10.6和v10.7
(64位应用程序)以及iOS 4
和iOS 5,
Xcode 4.2支持ARC。(弱引用不是$ b Mac OS X v10.6和iOS
支持$ b 4)。 Xcode
4.1中没有ARC支持。


-


如果我使用
ARC为iOS 5开发了一个新的应用程序,我是否需要实现一些
的复古兼容性检查?
I.e。:我需要检查iOS版本
并调用retain并相应发布
吗?所以,基本上,所有iOS版本都可以使用ARC
,iOS 5只需要
吗?


不,因为ARC在编译时而不是在运行时发挥其魔力。


而不是你必须记住
使用retain,release和
自动释放,ARC评估对象

生命周期要求,并在
编译时自动为你插入
适当的方法调用。编译器还
为你生成适当的dealloc方法


ARC的更多信息:http://clang.llvm.org/docs/AutomaticReferenceCounting.html


I'm currently developing an app for iPad. The development started for iOS 4.2 and is now continuing (and I think will be completed) for iOS 4.3. I just read about ARC in iOS 5, and basically I understood that we will never need to release and retain objects anymore. My questions are:

  1. If I decide to upgrade to iOS 5, do I need to remove all [myObject retain] and [myObject release] statements from my code?

  2. If I develop a new app for iOS 5 using ARC, will I need to implement some sort of "retro-compatibility" checks? i.e.: will I need to check the version of iOS and call retain and release accordingly? So, basically, is ARC available for all iOS versions or just for iOS 5?

解决方案

If I decide to upgrade to iOS 5, do I need to remove all [myObject retain] and [myObject release] statements from my code?

Yes, but XCode 4.2 includes a new "Migrate to Objective-C ARC" tool (in the Edit->Refactor menu), which does that for you. Calling dealloc is a different story. As mentioned in the comments the clang reference states that you should keep your the dealloc method:

Rationale: even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug.

You enable ARC using a new -fobjc-arc compiler flag. ARC is supported in Xcode 4.2 for Mac OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. (Weak references are not supported in Mac OS X v10.6 and iOS 4). There is no ARC support in Xcode 4.1.

-

If I develop a new app for iOS 5 using ARC, will I need to implement some sort of "retro-compatibility" checks? I.e.: will I need to check the version of iOS and call retain and release accordingly? So, basically, is ARC available for all iOS versions or just for iOS 5?

No, because ARC does its magic on compile time and not on run time.

Instead of you having to remember when to use retain, release, and autorelease, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls for you at compile time. The compiler also generates appropriate dealloc methods for you.

Further Information on ARC: http://clang.llvm.org/docs/AutomaticReferenceCounting.html

这篇关于关于iOS5 SDK中自动引用计数的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆