为什么Xcode为IBOutlet创建弱引用? [英] Why does Xcode create a weak reference for an IBOutlet?

查看:118
本文介绍了为什么Xcode为IBOutlet创建弱引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在故事板上创建插座时,它会生成以下代码__weak IBOutlet UILabel *mLabel;.

I notice when i create a outlet within a story board it generates the following code __weak IBOutlet UILabel *mLabel;.

为什么将其声明为弱指针?据我了解,当对象被释放时,其所有成员也将被释放.在我的大多数代码中,我都将出口声明为强指针.这会造成问题吗?

Why is it declaring it as a weak pointer? From my understanding, when the object gets released, all its members will get released too. In most of my code I'm declaring the outlets as strong pointers. Is this going to create problems?

推荐答案

根据iOS开发者库:

According to iOS Developer Library: link here

部分: Managing the Lifetimes of Objects from Nib Files

Section: Managing the Lifetimes of Objects from Nib Files

从实用的角度来看,在iOS和OS X中, 定义为声明的属性.出口通常应该比较弱, 除了从文件所有者到笔尖文件中的顶级对象的那些对象 (或者,在iOS中为分镜脚本场景),该效果应该很强.出口 因此,您创建的内容通常应该较弱,因为:

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should therefore typically be weak, because:

您为视图控制器的视图或视图的子视图创建的插座 例如,窗口控制器的窗口是任意引用 在不暗示所有权的对象之间.

Outlets that you create to subviews of a view controller’s view or a window controller’s window, for example, are arbitrary references between objects that do not imply ownership.

强大的渠道是 经常由框架类指定(例如, UIViewController的视图出口或NSWindowController的窗口 出口).

The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).

@property (weak) IBOutlet MyView *viewContainerSubview;

@property (strong) IBOutlet MyOtherClass *topLevelObject;

这篇关于为什么Xcode为IBOutlet创建弱引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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