简体   繁体   English

为什么默认情况下插座被声明为弱?

[英]Why are the outlets by default declared as weak?

I looked up this topic on google but have not got an understandable answer yet, the problem is that I know that when two classes are coupled together by instantiating an object in the first class from the second class and declaring another object in the second class from the first class this will cause a retain cycle that should be broken by using the keyword weak or unowned , yet I can not apply this way of thinking on the IBOutlets being declared as weak for example我在谷歌上查了这个话题,但还没有得到一个可以理解的答案,问题是我知道当两个类通过从第二个类中实例化第一个类中的一个对象并在第二个类中声明另一个对象来耦合在一起时第一个类这将导致一个保留循环,应该通过使用关键字 weak 或 unowned 来打破,但我不能将这种思维方式应用于被声明为弱的 IBOutlets

class SignUpViewController: UIViewController {
 override func viewDidLoad() {
        super.viewDidLoad()
    }
@IBOutlet weak var signUpBttn: UIButton!
}

this is an outlet in my viewController class, why the outlet is declared as weak ?这是我的 viewController 类中的插座,为什么插座被声明为 weak ? as per what I understand is that to have a retain cycle, the uibutton class should have an object from the viewController class so that the two classes (viewController and uibutton) become coupled together can anybody clarify what is happening under the hood?根据我的理解,要有一个保留周期,uibutton 类应该有一个来自 viewController 类的对象,以便这两个类(viewController 和 uibutton)耦合在一起有人可以澄清幕后发生的事情吗?

all ui element in a view controller are part of the viewController view.视图控制器中的所有 ui 元素都是 viewController 视图的一部分。 so view is a UIView class and outlets are reference in the viewController to UIView class elements, so if the view is removed form the view hierarchy all cross element should be weak to avoid cross reference.所以视图是一个 UIView 类,并且出口在 viewController 中引用到 UIView 类元素,所以如果从视图层次结构中删除视图,所有交叉元素都应该是弱的,以避免交叉引用。 thew eid problem about this is the reference from apple MVC, where a ViewController is a Controller but have a bunch o related code of the view part.关于这个的问题是来自苹果 MVC 的参考,其中 ViewController 是一个控制器,但有一堆视图部分的相关代码。 all you outlets should be placed in the UIView class of you ViewController.你所有的插座都应该放在你的 ViewController 的 UIView 类中。

TLDR - in my opinion it's not a good decision. TLDR - 在我看来,这不是一个好的决定。

A strong reference denotes ownership. strong引用表示所有权。 The view or view controller is the owner of the views therefore the logical choice should be strong .视图或视图控制器是视图的所有者,因此逻辑选择应该strong

However, in most situations this does not really matter because the views are also strongly referenced by their parent in the view hierarchy.然而,在大多数情况下,这并不重要,因为视图在视图层次结构中也被其父级强烈引用。

When does it matter?什么时候重要? It matters in situations when you are dynamically updating the view hierarchy by removing views/constraints etc. Once you remove a view/constraint from the hierarchy and you don't have a strong reference to it, it will get removed from memory.当您通过删除视图/约束等动态更新视图层次结构时,这很重要。一旦您从层次结构中删除视图/约束并且您没有对它的强引用,它将从内存中删除。

Also note that the combination of weak and !还要注意weak和的组合! is a bit dangerous because !有点危险,因为! denotes a reference that you expect never to be nil .表示您希望永远不会为nil的引用。

This can lead to errors, for example:这可能会导致错误,例如:

@IBOutlet weak var constraint: NSLayoutConstraint!

...

constraint.isActive = false // removes constraint from hierarchy, assigns `nil` to constraint
...
constraint.isActive = true // crashes the app

Personally, I always make outlets strong .就个人而言,我总是使网点strong For any weak references I always use ?对于我总是使用的任何weak引用? and not !而不是! . .

Note that weak in this case doesn't have anything to do with protection against reference cycles.请注意,在这种情况下, weak与防止引用循环无关。 It was just a personal decision by Xcode developers.这只是 Xcode 开发人员的个人决定。

Historically, there might be a connection with UIViewController.viewDidUnload .从历史上看,可能与UIViewController.viewDidUnload有联系。 However, that method is never called since iOS 6.但是,从 iOS 6 开始就不再调用该方法。

Apple recommends that an @IBOutlet be declared as strong . Apple 建议将@IBOutlet声明为strong Many discussions / articles can be found about this.可以找到许多关于此的讨论/文章。

If you take a look at this video from Apple's 2015 WWDC , right around the 32:30 mark: https://developer.apple.com/videos/play/wwdc2015/407/如果您看一下 Apple 2015 WWDC 的这段视频,就在 32:30 左右: https : //developer.apple.com/videos/play/wwdc2015/407/

He states:他说:

In general you should make your outlet strong, especially if you are connecting an outlet to a sub view or to a constraint that's not always going to be retained by the view hierarchy.一般来说,你应该让你的插座强大,尤其是当你将插座连接到子视图或约束时,视图层次结构并不总是会保留。 The only time you really need to make an outlet weak is if you have a custom view that references something back up the view hierarchy and in general that's not recommended.您真正需要使插座变弱的唯一时间是,如果您有一个自定义视图,该视图引用了视图层次结构中的某些内容,并且通常不推荐这样做。

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

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