简体   繁体   English

在 iOS 上使用 ARC 4,当使用 unsafe_unretained 而不是 weak 时,我是否需要 nil 我的 IBOutlet 属性?

[英]Using ARC on iOS 4, do I need to nil my IBOutlet properties when using unsafe_unretained instead of weak?

When using ARC with iOS 5, a weak IBOutlet creates a zeroing reference, avoiding the need to当将 ARC 与 iOS 5 一起使用时,弱 IBOutlet 创建一个归零参考,避免需要

self.< IBOutlet property > = nil;

in -(void)viewDidUnload-(void)viewDidUnload

If I'm using iOS 4 ( and using ARC ) and forced to use unsafe_unretained instead, does it mean I have to override viewDidUnload and set the property to nil manually?如果我使用 iOS 4(并使用 ARC )并被迫改用unsafe_unretained ,这是否意味着我必须覆盖viewDidUnload并手动将属性设置为 nil?

EDIT: This relates to my case: Should IBOutlets be strong or weak under ARC?编辑:这与我的案例有关: IBOutlets 在 ARC 下应该强还是弱? The exception being: I can't use the 'weak' keyword which creates the zeroing reference.例外情况是:我不能使用创建归零参考的“weak”关键字。

Hope my question is clearer.希望我的问题更清楚。

Thanks谢谢

When using ARC, as I am sure you have realized, the weak attribute cannot be used pre-iOS5.使用 ARC 时,我相信您已经意识到,在 iOS5 之前的版本中不能使用 weak 属性。 The other side of that coin would be to use unsafe_unretained.该硬币的另一面是使用 unsafe_unretained。 Weak attributes will automatically set your properties to nil.弱属性会自动将你的属性设置为 nil。 Unsafe_retained (aka "assign" in pre-iOS 5), will not, and you need to do this yourself. Unsafe_retained(在 iOS 5 之前的版本中也称为“分配”)不会,您需要自己执行此操作。

Without a property (in iOS) the IBOutlet will be ivar is set and retained by KVC.如果没有属性(在 iOS 中),IBOutlet 将由 KVC 设置和保留。 With a @property the ivar is set by setting the property.使用@property可以通过设置属性来设置ivar。

On an ARC project, if one creates a nib and drags an item (say UILabel ) to the.h file a strong @property will be added as well as in the.m file a line setting the property to nil will be added to the viewDidUnload method and a @synthesize statement for the property.在 ARC 项目中,如果创建一个 nib 并将一个项目(比如UILabel )拖到 .h 文件中,将添加一个强大的@property以及在 .m 文件中,将属性设置为 nil 的一行将添加到viewDidUnload方法和属性的 @synthesize 语句。

There are other ways to handle the retaining of nib IBOutlets that work and may even be better by some metric.还有其他方法可以处理nib IBOutlets的保留,这些方法有效,甚至可能在某些指标上更好。

From the Apple document Resource Programming Guide - Managing the Lifetimes of Objects from Nib Files :来自 Apple 文档资源编程指南 - 从 Nib 文件管理对象的生命周期

Because the behavior of outlets depends on the platform, the actual declaration differs:因为网点的行为取决于平台,实际声明不同:

For iOS, you should use:对于 iOS,您应该使用:
@property (nonatomic, retain) IBOutlet UserInterfaceElementClass *anOutlet; @property (nonatomic, retain) IBOutlet UserInterfaceElementClass *anOutlet;

For OS X, you should use:对于 OS X,你应该使用:
@property (assign) IBOutlet UserInterfaceElementClass *anOutlet; @property (assign) IBOutlet UserInterfaceElementClass *anOutlet;

My belief is to not fight the way Apple does things, doing so tends to make things harder.我的信念是不要像 Apple 做事的方式那样反抗,这样做往往会让事情变得更难。 Also consider that Apple has inside information of the future of the platform.还要考虑到 Apple 拥有该平台未来的内部信息。 :-) :-)

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

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