使用 ARC 并针对 iOS 4.0 时如何替换弱引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6893038/
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:54:56  来源:igfitidea点击:

How do I replace weak references when using ARC and targeting iOS 4.0?

iphoneiosautomatic-ref-counting

提问by Mason G. Zhwiti

I've begun developing my first iOS app with Xcode 4.2, and was targeting iOS 5.0 with a "utility application" template (the one that comes with a FlipsideViewController).

我已经开始使用 Xcode 4.2 开发我的第一个 iOS 应用程序,并使用“实用程序应用程序”模板(FlipsideViewController 附带的模板)针对 iOS 5.0。

I read that since ARC is a compile-time feature, it should be compatible with iOS 4 as well, so I attempted to target my app to 4.3, and try compiling it. When I do so, I get this error:

我读到,因为 ARC 是一个编译时功能,它也应该与 iOS 4 兼容,所以我尝试将我的应用程序定位到 4.3,并尝试编译它。当我这样做时,我收到此错误:

FlipsideViewController.m: error: Automatic Reference Counting Issue: The current deployment target does not support automated __weak references

FlipsideViewController.m:错误:自动引用计数问题:当前部署目标不支持自动 __weak 引用

It is referencing this line:

它引用了这一行:

@synthesize delegate = _delegate;

That variable is declared as:

该变量声明为:

@property (weak, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;

I understand that "weak references" are not supported in iOS 4, but I don't really understand why I would want to use a weak reference to begin with, nor can I figure out how I would rewrite things to avoid using it, while still taking advantage of ARC (after all, it's supposed to work with iOS 4 AND 5 right?)

我知道 iOS 4 不支持“弱引用”,但我真的不明白为什么我要使用弱引用开始,也无法弄清楚如何重写内容以避免使用它,而仍在利用 ARC(毕竟,它应该适用于 iOS 4 和 5,对吗?)

回答by Brad Larson

To target the older OS, you can use unsafe_unretainedinstead of weakin your property declaration, and it should mostly work the same way. weakreferences nil themselves when their target goes away, but unsafe_unretainedleaves open the possibility that the object you're linking to could turn into a dangling pointer when it is deallocated. The latter is the same behavior as if you had used assignas a property declaration in manual memory management.

要针对较旧的操作系统,您可以在属性声明中使用unsafe_unretained而不是weak,它应该以相同的方式工作。 weak当他们的目标消失时引用 nil 自己,但unsafe_unretained留下了你正在链接的对象在被释放时可能变成悬空指针的可能性。后者与您assign在手动内存管理中用作属性声明的行为相同。

You do this to avoid retain cycles, which I mention in my answer here. You don't want to have a strong pointer to something that might have a strong pointer back to the original object. Then nothing would get released properly.

您这样做是为了避免保留周期,我在此处的回答中提到了这一点。您不想拥有指向可能具有指向原始对象的强指针的强指针。那么什么都不会被正确释放。

回答by rpetrich

If only using weak references for additional safety, manually call the new runtime functions if they're available and fallback to simple assignment on __unsafe_unretainedvariables if not.

如果仅使用弱引用来提高安全性,请在可用时手动调用新的运行时函数,如果不可用则回退到对__unsafe_unretained变量进行简单赋值。

ZWRCompatibility.hwill simplify this somewhat.

ZWRCompatibility.h会稍微简化一下。

回答by nschum

Thanks to Mike Ash's compatibility library PLWeakCompatibilty, you can now simply use __weak on iOS 4.x, as well.

感谢 Mike Ash 的兼容性库PLWeakCompatibilty,您现在也可以在 iOS 4.x 上简单地使用 __weak 。

It's incredibly easy to configure and requires no additional consideration or effort over 5.x.

它非常容易配置并且不需要额外的考虑或超过 5.x 的努力。